Cross compile simple standard C program on Linux for Mac

2.5k views Asked by At

Cross compiling for Mac under Linux is a major PITA. To setup cross compilation you need an Apple developer account, the SDK from XCode etc. I tried it but couldn't get it to work. It seems all the available instructions are outdated anyway.

But the program I want to compile is just a small shell tool that would only need a standard C library (preferable statically linked glibc). No Mac Frameworks or Objective-C needed. It's easy to compile such a binary for Windows, not so much for OS X. It almost seems like all that is missing is the linking part, because you can build mach-o object files like that:

clang -target i386-apple-darwin-macho hello_world.c -o hello_world.o -c

But of course it's not that simple, because this generates an error:

echo $'#ifdef __linux__\n#error "thinks it\'s for linux"\n#endif'| \
    clang -x c - -o /dev/null -c

Is there any way to compile a mach-o object for Mac OS X with an unpatched clang under Linux? Is there then a way to link such an object file with a static glibc for Mac so it runs under OS X? And where would I get such an glibc (I guess I have to compile it myself somehow)?

1

There are 1 answers

2
Thomas On

My OSXCross project may be what you are looking for.

Is there any way to compile a mach-o object for Mac OS X with an unpatched clang under Linux?

Yes. Clang is a cross compiler by default. However, you will also need cctools/ld64 and the Mac OS X SDK.

Is there then a way to link such an object file with a static glibc for Mac so it runs under OS X?

There is no glibc for Mac OS X, it's simply called Libc there. No need to link it statically.

And where would I get such an glibc (I guess I have to compile it myself somehow)?

You'll need to get the Mac OS X SDK, there is no way around it. Even if you'd manage to build Libc by hand then you'd still lack a lot of required libraries.


I know that Mac OS X doesn't ship glibc.

There is no glibc for Mac OS X.

... but I write my code against that. E.g. I use PRIuPTR (%zu) etc., does Mac's libc support that?

Yes. AFAIK the %zu printf specifier works on everywhere besides Windows.

That's what I feared. Last time I tried that it was very cumbersome and I couldn't get it to work.

Get a free Apple developer account, then you can download Xcode via https://developer.apple.com/downloads/index.action.

Once you are done with that follow the SDK packaging instructions.

What libraries? A libc should only need to make syscalls and not use other libraries.

No.

$ otool -L /usr/lib/libc.dylib       
libc.dylib (architecture x86_64):
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)
        /usr/lib/system/libcache.dylib (compatibility version 1.0.0, current version 69.0.0)
        /usr/lib/system/libcommonCrypto.dylib (compatibility version 1.0.0, current version 60061.0.0)
        /usr/lib/system/libcompiler_rt.dylib (compatibility version 1.0.0, current version 35.0.0)
        /usr/lib/system/libcopyfile.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/system/libcorecrypto.dylib (compatibility version 1.0.0, current version 233.1.2)
        /usr/lib/system/libdispatch.dylib (compatibility version 1.0.0, current version 442.1.4)
        /usr/lib/system/libdyld.dylib (compatibility version 1.0.0, current version 353.2.1)
        /usr/lib/system/libkeymgr.dylib (compatibility version 1.0.0, current version 28.0.0)
        /usr/lib/system/liblaunch.dylib (compatibility version 1.0.0, current version 559.20.9)
        /usr/lib/system/libmacho.dylib (compatibility version 1.0.0, current version 862.0.0)
[And so on...]

If I can give you one good advice, do not even try to build Libc by hand. It's (almost) impossible to do so. Last time I tried a lot of headers were missing that I couldn't even find via opensource.apple.com.

It's just a waste of time.

And the C library is by far not all you will need for OS X, a lot of libraries depend on (closed source) Frameworks, libc++, ...