Search code examples
cgcclinkerlmldd

ldd shows libm.so.6 but still linking with -lm is needed


I built a library and ldd shows that it references libm.so.6:

 ldd liba.so 
        ...  
        libm.so.6 => /lib64/libm.so.6 (0x00007fdf53e85000)
        ...

But when I compile my program I get:

$ gcc mytest.c -I/path/to/a/header -L/home/path/to/a/so  -la 
/usr/bin/ld: undefined reference to symbol 'sin@@GLIBC_2.2.5'
//usr/lib64/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

And only if I append -lm at the end of the latter command, the compilation succeeds.

Why is it so, if libm.so.6 is shown by ldd? And why the error could contain /usr/lib64/libm.so.6 instead of /lib64/libm.so.6? Thank you.


Solution

  • ldd lists the shared objects required by liba.so - something that'll be used by the dynamic linker (ld.so). But the error you get is from the static linker (ld).

    That liba.so has a dependency on libm.so isn't relevant to ld when it attempts to resolve the symbols and link the executable together - despite the fact you link liba.so.

    To ld, liba.so is another library to look at for unresolved symbols - it doesn't look at all dependencies of liba.so.