Saturday, November 13, 2010

Building clang

So, I decided to get libc++ and use it instead of libstdc++.
They're both implementations of the C++ Standard Library.
For those who don't program: A Library contains stuff a programmer can use, so he doesn't have to write it for himself. For example algorithms for sorting a list of numbers.
The Standard Library is special to other libraries, because it's part of the C++ language.

I ran into a problem with the C++0x's random number generator, producing numbers outside the range [0..1], although I told it so. Supposedly this is a known problem, but since I cannot use clang with libstdc++ C++0x-features, I decided to get libc++ and use it instead of libstdc++.

When I installed clang, it came without clang++ (which basically is clang, except it adds some flags to properly compile C++ code) for some reason, and I always had to explicitly state to link against the C++ Standard Library. Since libc++ didn't seem easily configurable I downloaded the llvm (required to compile clang) and clang source codes to compile them myself and finally obtain clang++.

So, first I used gcc to compile it, then I wanted to use clang to compile them.
The first problem was some bug in the configuration utility when using automatically selected optimization flags. I found out through trial and error.
Then when I tried to compile with clang, it complained about the code not being linkable.
After a bit of trial and error I found out it was the maximum optimization flag (-O4); using the one beloq (-O3) it worked.
A little research and I found out that -O4 enables something called 'link-time optimization' and while usually the linker is independent of compiler optimizations, this of course requires a compatible linker.

Note: when compiling code, one usually means compile and link. Compiling turns individual source-code files into files with in intermediate language. Linking takes all those files in the intermediate language and turns them into the binary.

I installed the linker supporting it, still no avail. I found out I have to build llvm with that specific feature enabled, which required me to have the source-code of the linker so that llvm can use its link-time optimization features. I couldn't find the headers after I installed the dev and source packages, so I proceeded to download the source off their CVS and compile it myself.

Compiling the binutils bugs at some places using clang, but I got 'em all resolved.
Mostly complaining about unused results (due to -Werror) I at first wanted to fix by voiding the expressions, but because it was getting tedious work I simply added -Wno-error=unused-value.
And where I am now is compiling the gold-module (which actually will enable the link-time optimization support), failing at some special function attribute.

gcc is the one established C and C++ compiler (maybe for some other languages, too, I dunno). For Windows, MSVC is also quite popular. Sadly, this results in code not properly compilable with other compilers - such as clang.
This isn't the first library I have problems with. I'll just use gcc, but it's still kind of funny since certain coders are often anal about standard compliance.

No comments: