g++ linker: force static linking if static library exists?

限于喜欢 提交于 2019-11-27 16:59:52
naideflan
g++ -Wl,-Bstatic -lz -lfoo -Wl,-Bdynamic -lbar -Wl,--as-needed

Will link zlib and libfoo as static, and libbar as dynamic . --as-needed will drop any unused dynamic library.

stanthomas

When you only want to statically link one or two libraries with the rest, including system libraries, being dynamic, it is often easier to simply reference the static library by its full name. I.e. rather than use -l and -L to get g++ to resolve a library from what it finds, simpy add the full path to the library as an input. Taking the g++ command above, to link a main.o application main program to static libz and libfoo and dynamic libbar and libglib etc. :

    g++ main.o /usr/lib/libz.a /usr/lib/libfoo.a -lbar

Edit 3 Aug 17: I've just tripped across this answer which goes into more detail and offers an alternative way (-l:) to specify the library directly.

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