views:

428

answers:

2

I'm checking the portability of a shared object (.so) with the LSB AppChecker.
One of the problems it reports is that there is one external library (libm.so.6) that is not being used but is linked against anyways.

How can I prevent GCC from linking to this unneeded library?

EDIT:
The output of the ldd command against my shared object is:

    linux-gate.so.1 =>  (0x009ff000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x003dc000)
    libm.so.6 => /lib/libm.so.6 (0x00110000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00137000)
    libc.so.6 => /lib/libc.so.6 (0x0021d000)
    /lib/ld-linux.so.2 (0x0097f000)
A: 

Unless you're specifying -lm in your own link command, another library you're using is probably dependent on the math library. Judging by the fact that this question is tagged c++, you're probably getting it as a side effect of linking with libstdc++ and there's not much you can do.

% ldd /usr/lib/libstdc++.so.6                                                                                      
        linux-gate.so.1 =>  (0x4001e000)
        libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0x40127000)
        libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x4014d000)
        /lib/ld-linux.so.2 (0x40000000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x402b1000)
Bklyn
But the AppChecker is supposed to check one level of dependencies not two (i.e. not the dependencies of the dependencies). Either way, my shared object is linked against libm.so.6 too, and AppChecker says it doesn't need to. I updated my question with the output of ldd.
GetFree
It would be true, if `libstdc++` was compiled into the executable statically *and* did not use math interfaces. None of these is true.
Pavel Shved
+4  A: 

Pass the -Wl,-as-needed argument as part of the linker command line. This will automatically drop any direct library dependencies you're not actually using symbols from.

$ g++ -o test test.cpp -lm; readelf -d test|grep '(NEEDED)'
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
$ g++ -o test test.cpp -lm -Wl,-as-needed; readelf -d test|grep '(NEEDED)'                                                                    
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
bdonlan
At least on Linux g++ always adds '-lm' to the link line; you don't need explicit '-lm' to show the problem.
Employed Russian
Thanks. Now AppChecker doesn't complain about libm.so.6, and readelf doesn't show it either. The weird thing is that ldd still shows libm.so.6 as an external dependency. Anyone knows why?
GetFree
ldd shows libm.so.6 dependency, because libstdc++.so.6 depends on libm.so.6, and ldd shows both direct and indirect dependencies.To verify, run "ldd /usr/lib/libstdc++.so.6" or "readelf -d /usr/lib/libstdc++.so.6" -- you'll see libm in the list.
Employed Russian