After the Update to Fedora 20 I forgot to update the linker to Gold. Today I released that linking Samba is horribly slow. Time to change the linker to Gold again:
/usr/sbin/alternatives --set ld /usr/bin/ld.gold
/usr/sbin/update-alternatives --set ld /usr/bin/ld.gold
To still build a special project with ld.bfq use:
Since hermes is flooding my mailbox I haven’t looked if all my packages are compiling on Factory. So I had a look for interesting emails today. ctrlproxy (irc proxy/bouncer) didn’t build with undefined reference to ““. I wondered if the libarary packages have been renamed, but everything looked fine. I’ve talked to darix and he told me about the –as-needed flag of the linker which is set by default now. I’ve searched for some documentation didn’t find something which explains the problems so here is my documentation.
The problem is the linking order of the libraries and source code! Lets look at an example:
Assume that you built a static library libwurst and it uses the pow() function from the math library.
$ gcc -Wl,--as-needed --static main.c -o wurstsalat -L. -lm -lwurst
./libwurst.a(wurst.o): In function `wurst':
wurst.c:(.text+0x29): undefined reference to `pow'
The problem here is, that the linker doesn’t find any reference to pow() in main.c. Then the first library is libm and as it is not needed, the linker skips it. So you have to link against libwurst.a before you link against libm.
$ gcc -Wl,--as-needed --static main.c -o wurstsalat -L. -lwurst -lm
To summarize it: When using –as-needed, the order in which the libraries appear in the command line is relevant: any library X must precede all libraries Y that offer symbols that X uses.
I’ve created a page in the openSUSE wiki, you can find it here.