Search code examples
c#.netmacosmonomkbundle

Mkbundle failed to compile C# application on OS X


I've been at this for a few days now and whilst I fairly quickly managed to get Mono's mkbundle to create a native executable on Windows and Linux I haven't been able to do the same for OS X.

I'm using Mono 3.4.0 as I need .NET 4.5 support and being able to compile for x86_64 on OS X would be nice too.

I currently use the following simple bash script to attempt the mkbundle build:

#!/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

I get the following output for it:

$ ./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]

Things I've tried:

  • I found that removing the --static argument allows the mkbundle build to succeed. But that just moves to above problem to runtime, and I'd like it statically compiled anyway.
  • The Mono 3.4.0 release notes mention that mkbundle on Mac OS X now must always link against the Objective-C Runtime. However I cannot figure out how to do so and it doesn't make sense to me in why I'd want to do that.

Solution

  • I worked out what the issue was in the end - I saw running into both the new mkbundle property requirements and some mismatched architecture settings. The below script worked for me:

    #!/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