如何强制ld使用静态lib而不是共享lib? [英] How to force ld to use a static lib instead of shared lib?
问题描述
我试图使用静态版本的测试库来源。我有libtest.a和libtest.so可用,所以我使用-static选项。但是,看起来gcc链接器也试图搜索静态版本的标准数学库。
任何想法我可以用来链接标准库的共享版本的选项?
g ++ -static main。 cpp -o a.out -L。 -ltest
错误:
/ usr / bin / ld:找不到-lm
如果要强制链接器使用特定库的静态版本,您可以使用:filename
强制使用特定库给链接器一个'base'库名,并让它使用它找到的第一个:
g ++ main.cpp -o a .out -l:./ libtest.a
从 http://sourceware.org/binutils/docs-2.23.1/ld/Options.html :
-l namespec
pre>
--library = namespec
将
namespec
指定的归档或目标文件添加到要链接的
文件的列表中。此选项可以使用任意次数。如果
namespec
的格式为:filename
,ld将搜索库路径
a文件filename
,否则将在库路径中搜索
a文件libnamespec.a
p>
在支持共享库的系统上,ld还可以搜索除
libnamespec.a
之外的
文件。具体来说,在ELF和SunOS
系统上,ld将搜索一个名为
libnamespec.so
的库,然后再搜索一个名为libnamespec.a
。 (由
约定,.so扩展指示一个共享库。)注意
此行为不适用于:filename
文件名为filename
。
I am trying to build by source using the static version of the test library. I have both libtest.a and libtest.so available, so I am using the "-static" option. However, It looks like the gcc linker is also trying to search for static version the standard math library. Any idea what option I can use to link the shared versions of the standard libraries?
g++ -static main.cpp -o a.out -L. -ltest
Error:
/usr/bin/ld: cannot find -lm
If you want to force the linker to use the static version of a particular library you can use the :filename
to force a particular library instead of just giving the linker a 'base' library name and letting it use the first one it finds:
g++ main.cpp -o a.out -l:./libtest.a
From http://sourceware.org/binutils/docs-2.23.1/ld/Options.html:
-l namespec --library=namespec
Add the archive or object file specified by
namespec
to the list of files to link. This option may be used any number of times. Ifnamespec
is of the form:filename
, ld will search the library path for a file calledfilename
, otherwise it will search the library path for a file calledlibnamespec.a
.On systems which support shared libraries, ld may also search for files other than
libnamespec.a
. Specifically, on ELF and SunOS systems, ld will search a directory for a library calledlibnamespec.so
before searching for one calledlibnamespec.a
. (By convention, a .so extension indicates a shared library.) Note that this behavior does not apply to:filename
, which always specifies a file calledfilename
.
这篇关于如何强制ld使用静态lib而不是共享lib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!