直接告诉 gcc 静态链接库 [英] Telling gcc directly to link a library statically
问题描述
使用 -Wl,-Bstatic
来告诉 gcc
我想静态链接哪些库对我来说感觉很奇怪.毕竟我是直接告诉 gcc
关于链接库的所有其他信息(-Ldir
、-llibname
).
It feels strange to me to use -Wl,-Bstatic
in order to tell gcc
which libraries I want to link with statically. After all I'm telling gcc
directly all other information about linking with libraries (-Ldir
, -llibname
).
是否可以直接告诉 gcc 驱动程序应该静态链接哪些库?
Is it possible to tell the gcc driver directly which libraries should be linked statically?
澄清:我知道如果某个库只存在于静态版本中,它将在没有 -Wl,-Bstatic
的情况下使用它,但我想暗示 gcc
更喜欢静态库.我也知道直接指定库文件会与其链接,但我更喜欢保持包含静态和动态库的语义相同.
Clarification: I know that if a certain library exists only in static versions it'll use it without -Wl,-Bstatic
, but I want to imply gcc
to prefer the static library. I also know that specifying the library file directly would link with it, but I prefer to keep the semantic for including static and dynamic libraries the same.
推荐答案
使用 -l:
而不是 -l
.例如 -l:libXYZ.a
与 libXYZ.a
链接.注意 lib
和 .a
被写出,而不是 -lXYZ
会自动扩展到 libXYZ.so
/libXYZ.a
.
Use -l:
instead of -l
. For example -l:libXYZ.a
to link with libXYZ.a
. Notice the lib
and .a
are written out, as opposed to -lXYZ
which would auto-expand to libXYZ.so
/libXYZ.a
.
它是一个 GNU ld
链接器的选项一个>:
It is an option of the GNU ld
linker:
-l namespec
... 如果 namespec 的形式为 :filename
,ld
将搜索名为filename 的文件的库路径,否则它将搜索名为libnamespec.a
的文件的库路径.... 在 ELF ... 系统上,ld
将在目录中搜索名为 libnamespec.so
的库,然后再搜索名为 libnamespec.a
的库>.... 请注意,此行为不适用于 :filename
,它始终指定一个名为 filename 的文件."
-l namespec
... 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 calledlibnamespec.a
. ... on ELF ... systems,ld
will search a directory for a library calledlibnamespec.so
before searching for one calledlibnamespec.a
. ... Note that this behavior does not apply to:filename
, which always specifies a file called filename."
(自 binutils 2.18 起)
请注意,这仅适用于 GNU 链接器.如果你的 ld
不是 GNU 的那你就不走运了.
Note that this only works with the GNU linker. If your ld
isn't the GNU one you're out of luck.
这篇关于直接告诉 gcc 静态链接库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!