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

ぃ、小莉子 提交于 2019-12-05 02:40:54

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/2643156/how-to-make-shared-library-from-a-static-library-under-ubuntu-using-gcc

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