Does g++ linker optimizes unused libraries out when creating shared libraries?

160 views Asked by At

I have an object file and I want to create a shared library, libA.so, out of it and link it to another shared library, libB.so. The source file contains a single class and does not have any dependencies on libB.so.

I would create libA.so using either clang++ or g++:

  1. clang++ -shared -L/PATH/TO/LIBB -Wl,-soname=libA.so -o main.so main.o -lB
  2. g++ -shared -L/PATH/TO/LIBB -Wl,-soname=libA.so -o main.so main.o -lB

Using objdump or readelf I can see that my library is listed in the first case (clang), but not in the second case (g++). Any ideas why?

Example

C++ file:

#include <iostream>

class Tain {
public:
  void test() {
    std::cout << "YES";
  }
};

int main( int argc, const char* argv[] )
{
  Tain t;
  t.test();
}

Result of readelf -a main.so | grep -i needed:

  1. g++ -shared -L/PATH/TO/BOOST -lboost_log -Wl,-soname=main.so -o main.so main.cpp:

    0x0000000000000001 (NEEDED) Shared library: [libstdc++.so.6]

    0x0000000000000001 (NEEDED) Shared library: [libc.so.6]

  2. clang++-3.5 -shared -L/PATH/TO/BOOST -lboost_log -Wl,-soname=main.so -o main.so main.cpp:

    0x0000000000000001 (NEEDED) Shared library: [libboost_log.so.1.56.0]

    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]

0

There are 0 answers