C++ Statically linked shared library

萝らか妹 提交于 2019-11-28 17:53:13
nos

You shouldn't use the -static flag when creating a shared library, it's for creating statically linked executables.

If you only have a static version of the library, you can just link it in using -lsqlite3. But if there's both a dynamic version(.so) and a static version, the linker will prefer the dynamic one.

To instruct the linker to pick the static one, give the linker the -Bstatic flag, and make it switch back to dynamic linking for other stuff (like libc and dynamic runtime support) with -Bdynamic. That is, you use the flags:

 -Wl,-Bstatic -lsqlite3 -Wl,-Bdynamic 

Alternativly, you can just specify the full path of the .a file, e.g. /usr/lib/libsqlite3.a instead of any compiler/linker flags.

With the GNU ld, you can also use -l:libsqlite3.a instead of -lsqlite3. This will force the use of the library file libsqlite3.a instead of libsqlite3.so, which the linker prefers by default.

Remember to make sure the .a file have been compiled with the -fpic flag, otherwise you normally can't embed it in a shared library.

Any code that will somehow make its way into a dynamic library should be relocatable. It means that everything that is linked with your .so, no matter statically or dynamically, should be compiled with -fPIC. Specifically, static sqlite library should also be compiled with -fPIC.

Details of what PIC means are here: http://en.wikipedia.org/wiki/Position-independent_code

I had the same problem. Apparently -static is not the same as -Bstatic. I switched to -Bstatic and everything worked.

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