clinuxgccshared-librariesxlc

Split generation of a Shared Object library (.so) file into two pass instead of one


I am trying to build a shared library lib_test.so from 'test.c' & test.exp files. This lib_test.so file will be used as a extension to another application. The application doc specifies generation of tle lib_test.so file directly in a single pass by the following command:

`gcc -q64 -o lib_test.so test.c -bM:Sre -bE:test.exp -bnoentry`

But my requirement is to build the library in two passes:

  1. Compile to generate test.o file using gcc command.
  2. Link to generate the library lib_test.so using ld command.

I tried this as follows:

  1. Executed compile step as follows: gcc -q64 -c -o test.o test.c.
  2. Create lib_test.so as follows: ld -bM:Sre -bE:test.exp -bnoentry -o lib_test.so test.o

But it is not generating a proper lib_test.so file.

I am using Ubuntu 16.04 LTS 64-Bit with latest GCC

Can you please suggest the correct way to split the process into two passes...

Thanks & Regards.


Solution

  • The big difference between linking with the GCC frontend program gcc and with the actual linker ld is that the GCC frontend adds a few libraries to be linked with. Most notably the GCC runtime library (-lgcc_s or -lgcc) and the actual standard C library (-lc).

    When you invoke ld directly you do not tell it to link with those libraries.

    There might also be other libraries and flags the GCC frontend passes to ld without your knowledge. For the "one pass" build, pass the flag -v to gcc for verbose output and see what arguments, flags and libraries it uses.