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)?
My OSXCross project may be what you are looking for.
Yes. Clang is a cross compiler by default. However, you will also need cctools/ld64 and the Mac OS X SDK.
There is no glibc for Mac OS X, it's simply called
Libc
there. No need to link it statically.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.There is no glibc for Mac OS X.
Yes. AFAIK the
%zu
printf specifier works on everywhere besides Windows.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.
No.
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++, ...