Search code examples
c++g++ld

How g++ links in required libraries?


Let's consider the usual command for building libA.so that depends on libB.so, libC.so, libD.so:

$g++ -shared -L/path/to/libB -L/path/to/libC -L/path/to/libD
-lB -lC -lD
-I/path/to/headers 
 libA.cpp -o libA.so

Is there a mapping between the undefined symbols and the required library names:

undefined_symbol_1 comes from libB.so
undefined_symbol_2 comes from libC.so
undefined_symbol_3 comes from libC.so
undefined_symbol_4 comes from libC.so
undefined_symbol_5 comes from libD.so

or are they just kept separately:

Undefined symbols: undefined_symbol_1, undefined_symbol_2, undefined_symbol_3, undefined_symbol_4, undefined_symbol_5; Required libraries: "libB.so", "libC.so", "libD.so";


Solution

  • The library contains the undefined symbols and the library information they from.

    You could list the information in various ways. One method being objdump.

    For example looking at say libcurl dymanic symbols:

    > objdump -T /usr/lib/x86_64-linux-gnu/libcurl.so
    0000000000000000      DF *UND*  0000000000000000  GLIBC_2.2.5 pthread_detach
    0000000000000000      DF *UND*  0000000000000000  NETTLE_6    nettle_md4_init
    .
    .
    

    The name such as GLIBC_2.2.5 is defined in the version information which can be seen running

    > objdump -x /usr/lib/x86_64-linux-gnu/libcurl.so
    .
    .
    Version References:
    .
    required from libnettle.so.6:
      0x0a991006 0x00 06 NETTLE_6
    required from libc.so.6:
      0x09691a75 0x00 05 GLIBC_2.2.5
    .