Monday, November 22, 2010

Building g++

Trying to go down the clang-route failed. libc++ supposedly is not in a working state, at least for Linux.

So then I decided to build a trunk-version of g++ (well, gcc, g++ and libstdc++). Said, done. I also build it with LTO support.

The trunk of gcc is 1.3GB, the build-directory 1.9GB according to du.
Timing of the build (just the call to make, not including the call to configure and make install):

real 98m55.125s
user 101m28.610s
sys 5m39.180s

g++ -v output:

z33ky@mobileblahbuntu:~$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.6.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../configure --enable-gold=both --enable-languages=c,c++ --with-gold --enable-build-with-cxx --enable-lto --disable-multilib
Thread model: posix
gcc version 4.6.0 20101120 (experimental) (GCC)

Attempting to build my current project failed after that, but the errors were quickly fixed.
Now I am re-writing a small part of the code making use of unrestricted unions, where I had a union of pointers before.
Yawn!! BORING!!

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.