FC5, binutils-2.16.91.0.6-5: As discussed in https://www.redhat.com/archives/fedora-devel-list/2006-October/msg00724.html, FC5's ld --as-needed doesn't seem to handle undefined non-weak references in dependent libs, while it reportedly does in RHEL4. Example (as noted in the ML discussion, don't pay attention to the fact that libgtk should probably not have those undefined in the first place but should link with libgdk - this is just for demonstration purposes): foo.c: extern void gtk_main(); int main() { gtk_main(); } $ gcc -Wl,--as-needed foo.c `pkg-config --libs gtk+` $ ./a.out ./a.out: symbol lookup error: /usr/lib64/libgtk-1.2.so.0: undefined symbol: gdk_root_window Shouldn't the linker find out that linking with libgdk is actually needed in this case and not cause --as-needed to drop it?
That's the intended and documented behavior, info ld says: `--as-needed' `--no-as-needed' This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the `--as-needed' option. Normally, the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed. `--as-needed' causes DT_NEEDED tags to only be emitted for libraries that satisfy some symbol reference from regular objects which is undefined at the point that the library was linked. `--no-as-needed' restores the default behaviour. The only difference between FC5 and FC6 binutils in this area is that FC6 binutils issues an error in this case at link time, but FC5 does not and you only see the failure at runtime. On FC6: echo 'void foo (void) { bar (); }' | gcc -shared -O2 -o liba.so -xc - -fpic echo 'void bar (void) { }' | gcc -shared -O2 -o libb.so -xc - -fpic echo 'int main (void) { foo (); }' | gcc -O2 -o c -xc - -Wl,-rpath,.,--as-needed -L. -la -lb [ -x c ] && ./c shows: ./liba.so: undefined reference to `bar' collect2: ld returned 1 exit status on FC5: ./c: symbol lookup error: ./liba.so: undefined symbol: bar