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

标签 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 运行时。但是,我不知道该怎么做,而且我不明白我为什么要这样做。

最佳答案

我最终弄清楚了问题所在 - 我看到遇到了新的 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

关于c# - Mkbundle 无法在 OS X 上编译 C# 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361498/

相关文章:

macos - 运行 go 给我 - go clang : error: no input files

cocoa - Mac/Cocoa 中的套接字编程

c - 当我尝试在 Linux 上编译而不是在 Mac 上编译时,为什么会收到 "error: implicit declaration of function ‘fileno’

c# - 在 Dotnet Framework 4.8 客户端中使用 Core gRPC 服务器?

c# - c#中属性的单例模式

c# - 有没有办法在c#中注释掉字符串的一部分?

c# - 你如何阅读 lambda 表达式?

c# - 从方法引用 C# 获取方法信息

c# - DataContractJsonSerializer DateTime 隐式时区转换

c# - 在 C# 中查找大图像中的子图像