Can I build a shared library by linking static libraries?

我怕爱的太早我们不能终老 提交于 2019-11-27 14:51:01

问题


I have a bunch of static libraries (*.a), and I want to build a shared library (*.so) to link against those static libraries (*.a). How can I do so in gcc/g++?


回答1:


You can (just extract all the .o files and link them with -shared to make a .so), but whether it works, and how well it works, depends on the platform and whether the static library was compiled as position-independent code (PIC). On some platforms (e.g. x86_64), non-PIC code is not valid in shared libraries and will not work (actually I think the linker will refuse to make the .so). On other platforms, non-PIC code will work in shared libraries, but the in-memory copy of the library is not sharable between different programs using it or even different instances of the same program, so it will result in HUGE memory bloat.




回答2:


I can't see why you couldn't just build the files of your dynamic library to .o files and link with;

gcc -shared *.o -lstaticlib1 -lstaticlib2 -o mylib.so


来源:https://stackoverflow.com/questions/8808691/can-i-build-a-shared-library-by-linking-static-libraries

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