简体   繁体   English

如何强制ld使用静态库而不是共享库?

[英]How to force ld to use a static lib instead of shared lib?

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. 我同时具有libtest.a和libtest.so,因此我使用的是“ -static”选项。 However, It looks like the gcc linker is also trying to search for static version the standard math library. 但是,看来gcc链接程序也在尝试搜索标准数学库的静态版本。 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: 如果要强制链接器使用特定库的静态版本,则可以使用:filename强制特定库,而不仅仅是给链接器提供“基本”库名,并让它使用找到的第一个库:

g++ main.cpp -o a.out -l:./libtest.a

From http://sourceware.org/binutils/docs-2.23.1/ld/Options.html : 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. namespec指定的归档文件或目标文件添加到要链接的文件列表中。 This option may be used any number of times. 此选项可以使用多次。 If namespec is of the form :filename , ld will search the library path for a file called filename , otherwise it will search the library path for a file called libnamespec.a . 如果namespec的格式为:filename ,则ld将在库路径中搜索名为filename ,否则ld将在库路径中搜索名为libnamespec.a的文件。

On systems which support shared libraries, ld may also search for files other than libnamespec.a . 在支持共享库的系统上,ld可能还会搜索libnamespec.a以外的文件。 Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a . 具体来说,在ELF和SunOS系统上,ld将在目录中搜索名为libnamespec.so的库, libnamespec.so再搜索一个名为libnamespec.a (By convention, a .so extension indicates a shared library.) Note that this behavior does not apply to :filename , which always specifies a file called filename . (按照惯例,.so扩展名表示共享库。)请注意,此行为不适用于:filename ,后者始终指定一个名为filename

I've never used Michael's suggestion, but I will be tucking it away for future use. 我从未使用过Michael的建议,但我会将其藏起来以备将来使用。

The technique I use to fully control library linking is to avoid -L , l , -Bstatic and -Bdynamic altogether by fully specifying the library I want to use. 我用来完全控制库链接的技术是通过完全指定我要使用的库来完全避免-Ll-Bstatic-Bdynamic The command would look similar to: 该命令看起来类似于:

g++ main.cpp -o a.out /usr/local/lib/test.a

or 要么

g++ main.cpp -o a.out /usr/local/lib/test.so

or 要么

g++ main.cpp -o a.out /usr/local/lib/test.so.1.0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM