Mkbundle无法在OS X上编译C#应用程序

时间:2014-06-23 08:29:58

标签: c# .net macos mono mkbundle

我已经在这几天了,虽然我很快就设法让Mono的mkbundle在Windows和Linux上创建了一个本机可执行文件但我还没有能够做到这一点对于OS X也一样。

我使用的是Mono 3.4.0,因为我需要.NET 4.5支持,并且能够在OS X上编译x86_64也不错。

我目前使用以下简单的bash脚本来尝试mkbundle构建:

#!/bin/bash
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig:/Library/Frameworks/Mono.framework/Versions/3.4.0/lib/pkgconfig
#PKG_CONFIG_PATH=/Library/Frameworks/Mono.framework/Versions/3.4.0/lib/pkgconfig

mkbundle bin/Release/Launcher.exe --static --deps -o bin/Release/OSXNative/Launcher.exe

我得到了以下输出:

$ ./OSXNativeBuild.sh 
OS is: Darwin
Note that statically linking the LGPL Mono runtime has more licensing restrictions than dynamically linking.
See http://www.mono-project.com/Licensing for details on licensing.
Sources: 1 Auto-dependencies: True
   embedding: /Volumes/app/Launcher/Launcher/bin/Release/Launcher.exe
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/4.5/mscorlib.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/Mono.Security/4.0.0.0__0738eb9f132ed756/Mono.Security.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.Configuration/4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.Xml/4.0.0.0__b77a5c561934e089/System.Xml.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.Security/4.0.0.0__b03f5f7f11d50a3a/System.Security.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.Windows.Forms/4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.Drawing/4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/Mono.WebBrowser/4.0.0.0__0738eb9f132ed756/Mono.WebBrowser.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/Mono.Posix/4.0.0.0__0738eb9f132ed756/Mono.Posix.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.Runtime.Serialization.Formatters.Soap/4.0.0.0__b03f5f7f11d50a3a/System.Runtime.Serialization.Formatters.Soap.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/Accessibility/4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.Data/4.0.0.0__b77a5c561934e089/System.Data.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/Mono.Data.Tds/4.0.0.0__0738eb9f132ed756/Mono.Data.Tds.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.Transactions/4.0.0.0__b77a5c561934e089/System.Transactions.dll
   embedding: /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/mono/gac/System.EnterpriseServices/4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll

Compiling:
as -o temp.o temp.s 
cc -o bin/Release/OSXNative/Launcher.exe -Wall `pkg-config --cflags mono-2` temp.c  `pkg-config --libs-only-L mono-2` `pkg-config --variable=libdir mono-2`/libmono-2.0.a `pkg-config --libs-only-l mono-2 | sed -e "s/\-lmono-2.0 //"` temp.o
ld: warning: ignoring file /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/pkgconfig/../../lib/libmono-2.0.a, file was built for archive which is not the architecture being linked (x86_64): /Library/Frameworks/Mono.framework/Versions/3.4.0/lib/pkgconfig/../../lib/libmono-2.0.a
Undefined symbols for architecture x86_64:
  "_mono_main", referenced from:
      _main in temp-I71V1m.o
  "_mono_register_bundled_assemblies", referenced from:
      _mono_mkbundle_init in temp-I71V1m.o
  "_mono_set_dirs", referenced from:
      _main in temp-I71V1m.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ERROR: [Fail]

我尝试的事情:

  • 我发现删除--static参数允许mkbundle构建成功。但这只是将问题转移到运行时,我还是喜欢静态编译。
  • Mono 3.4.0 release notes提到Mac OS X上的mkbundle现在必须始终链接到Objective-C运行时。但是我无法弄清楚如何这样做,对我来说,为什么我不想这样做是没有意义的。

1 个答案:

答案 0 :(得分:6)

我最终解决了问题所在 - 我看到了新的mkbundle属性要求和一些不匹配的架构设置。以下脚本对我有用:

#!/bin/bash

# Notes:
# You will need the Mono SDK 3.x series as the 2.x SDK cannot build for 64bit architectures.
# It appears that Mono 3.x still cannot compile for 64bit without having to manually compile Mono yourself.
# Since we don't strictly need 64bit support the below forces a 32bit build.

# Ensure it can find pkg-config:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig:/Library/Frameworks/Mono.framework/Versions/3.4.0/lib/pkgconfig

# Force 32bit build and manually set some clang linker properties:
export AS="as -arch i386"
export CC="cc -arch i386 -lobjc -liconv -framework Foundation"

# Build:
mkbundle bin/Release/Launcher.exe --static --deps -o bin/Release/OSXNative/Launcher.exe