undefined reference to “function name”
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 “
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.