Hema Joshi
Hema Joshi

Reputation: 247

how to make shared library from a static library under ubuntu using gcc

i have a static library libsrp.a, i want to make a shared library libsrp.so from it that contains all symbols . please tell me how to make a .so under ubuntu.

thanks

Upvotes: 3

Views: 10039

Answers (2)

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

Use the --whole-archive flag:

gcc -shared -o libsrp.so -Wl,--whole-archive -lsrp -Wl,--no-whole-archive

From the ld man page (my emphasis):

--whole-archive For each archive mentioned on the command line after the --whole-archive option, include every object file in the archive in the link, rather than searching the archive for the required object files. This is normally used to turn an archive file into a shared library, forcing every object to be included in the resulting shared library. This option may be used more than once.

Upvotes: 3

Jasmeet
Jasmeet

Reputation: 2352

Recompile the object files contained in libsrp.a with the flag to create position independent code (fpic) as in

gcc -fpic -c foo.c
gcc -fpic -c bar.c

Now you can combine foo.o and bar.o into a shared library as in

gcc -shared -o libshared.so foo.o bar.o

Upvotes: 3

Related Questions