how to force linker to use shared library instead of static library?

陌路散爱 提交于 2019-11-29 11:26:30

问题


This is a quote from Linux programming book:


% gcc -o app app.o -L. –ltest

Suppose that both libtest.a and libtest.so are available.Then the linker must choose one of the libraries and not the other.The linker searches each directory (first those specified with -L options, and then those in the standard directories).When the linker finds a directory that contains either libtest.a or libtest.so, the linker stops search directories. If only one of the two variants is present in the directory, the linker chooses that variant. Otherwise, the linker chooses the shared library version, unless you explicitly instruct it otherwise.You can use the -static option to demand static archives. For example, the following line will use the libtest.a archive, even if the libtest.so shared library is also available:

% gcc -static -o app app.o -L. –ltest


Since if the linker encounters the directory that contains libtest.a it stops search and uses that static library, how to force the linker to search only for shared library, and not for static?

% gcc -o app app.o -L. libtest.so ?


回答1:


You could use -l option in its form -l:filename if your linker supports it (older versions of ld didn't)

gcc -o app app.o -L. -l:libtest.so

Other option is to use the filename directly without -l and -L

gcc -o app app.o /path/to/library/libtest.so



回答2:


from the man :

-shared-libgcc
-static-libgcc
On systems that provide libgcc as a shared library, these options force the use of either the shared or static version respectively. If no shared version of libgcc was built when the compiler was configured, these options have no effect.

good luck



来源:https://stackoverflow.com/questions/4422493/how-to-force-linker-to-use-shared-library-instead-of-static-library

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