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 ““. 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.

5 thoughts on undefined reference to “function name”

  1. What to do if libfoo.so provides foo() but needs bar() and libbar.so provides bar() but needs foo()? I have seen such pathetic set of dependencies in the past!

  2. When the change was announced by coolo at opensuse-factory ML he linked to the docs from Gentoo that already have a section “Importance of linking order” (http://www.gentoo.org/proj/en/qa/asneeded.xml#doc_chap2)

    Also, see “-lnamespec” in ld man, it clearly specified that the linking order is important. If upstream fails to set the correct order it’s a bug on their part, report it!!
    For Nikanth’s case… I think you can just use -lfoo -lbar -lfoo, but -lnamepec suggests the use of “-(“.

  3. What to do if libfoo.so provides foo() but needs bar() and libbar.so provides bar() but needs foo()? I have seen such pathetic set of dependencies in the past!

  4. It should work too. I think you have to test it or compile it without –as-needed. It would be better to fix it. I think this is a design error.

Leave a Reply