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

不问归期 提交于 2019-12-03 17:17:11
Jasmeet

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!