This is the second part of a serie of article on building a universal compiler.
On the first part, we use clang to compile for linux, windows, mac and ios from linux.
But the compiler was not able to link the binary / library.
clang build a binary from 2 steps (That’s not exactly the truth ;) ) :
- the assembler (generate the object file)
- the linker (generate the binary)
You can see the different phases using the command:
$ /opt/compiler/linux/bin/clang -ccc-print-phases -ccc-host-triple i386-apple-darwin -o hello.o -c hello.c
0: input, "hello.c", c
1: preprocessor, {0}, cpp-output
2: compiler, {1}, assembler
3: assembler, {2}, object
4: bind-arch, "i386", {3}, object
So the first 3 steps work fine. The last one is more complex as it needs the ld command, and the native libraries.
I spend some time looking for this and found some open source projects that provide this.
First I found odcctools from the opendarwin project (opensource project based on MacOsX from apple). Unfortunatly this project is shutting down in 2006.
Then I found that the official cctools from apple is open source, and can be download from http://www.opensource.apple.com/source/cctools/
Building cctools
It’s opensource… but not so easy to build without a mac! There are some project on github that help for that stuff. Take one of them, for exemple https://github.com/tatsh/xchain
After this compilation, I try to compile hello world test:
$ /opt/compiler/linux/bin/clang -ccc-host-triple i386-apple-darwin -o hello hello.c --sysroot /opt/compiler/mac/Developer/SDKs/MacOSX10.4u.sdk/ -B/opt/compiler/mac/bin $ file hello hello: Mach-O executable i386
So simple !!! I am now able to compile for MacOSX from linux.
Some troubles…
Of course, It won’t be so easy ;)
The compilater works, but it depends of the clang version you are using…
In my case, I am working with clang 3.1 (trunk) as I want to have the same version as the one in my mac 10.7. But the xchain project on github is compiling cctools-806 (so an older version).
On some projects, (like libpng), I have some linking problems:
/opt/compiler/mac/bin/ld: .libs/libpng15_la-png.o unknown flags (type) of section 12 (__TEXT,__literal16) in load command 0 /opt/compiler/mac/bin/ld: .libs/libpng15_la-pngerror.o unknown flags (type) of section 12 (__TEXT,__literal16) in load command 0
I will work on the compilation of cctools-822…
EDIT: I just realize my mistake: ld was link to ld_classic. But ld64 should be used. So it work like a charm ;)


Develop a mobile web app in HTML, JS, CSS, Ajax and jeMbe API. Then compile it into .ipa (iPad, iPhone) or .apk (Android) with jeMbe software


