--as-needed and undefined non-weak symbols

Adam Jackson ajackson at redhat.com
Fri Oct 27 17:03:16 UTC 2006

Ville Skyttä wrote:
> I'm wondering if I've stumbled upon a bug in ld's --as-needed or
> something I was not aware of, here's an example, foo.c:
> extern void gtk_main();
> int main() {
>     gtk_main();
> }
> Now, libgtk (as of FC5) contains lots of undefined gdk_* symbols, so
> trying "gcc -lgtk foo.c" on the above fails as expected, and "gcc -lgdk
> -lgtk foo.c" succeeds, again as expected.  However, to my surprise, "gcc
> -Wl,--as-needed -lgdk -lgtk foo.c" fails:
> /tmp/ccImoouo.o: In function `main':
> foo.c:(.text+0xa): undefined reference to `gtk_main'
> collect2: ld returned 1 exit status
> Is this how --as-needed should behave?  Ie. the linker does not appear
> to notice that linking with libgdk is actually needed in this case and
> drops it -> boom.

Yes.  Linking goes in order.  gdk is scanned, then gtk, then foo.c, and 
then implicit libc at the end.  foo.c references symbols in gtk, but 
you've already scanned it, and since you asked for as-needed semantics 
there was nothing already mentioned that referenced gtk.

- ajax

More information about the fedora-devel-list mailing list