关于 GCC 链接器的问题
抱歉,因为目前我没有环境来自己实验和整理以下问题:
1)假设我有四个库文件: libmylib_super.a
和 libmylib_super.so< /code>、
mylib_dumb.a
和 mylib_dumb.so
。在指定要链接的库时,以下方法之间有什么区别:
A) -l:libmylib_super.a
B) -llibmylib_super
C) -lmylib_super
D) -lmylib_dumb
2) 手册页中 -static
的定义:
在支持动态的系统上 链接,这可以防止链接 共享库。关于其他 系统,此选项无效。
这个链接器选项与问题#1 有什么关系吗?或者说……万一他们会互相干扰呢?
谢谢。
--- 于 2009-12-28 编辑 ---
我刚刚通过链接到 Boost date_time 库建立了我的环境并进行了一些实验。假设我有三个库文件:libboost_date_time-mt-da
、libboost_date_time-mt-d.so.1.41.0
、libboost_date_time-mt-d.so - > libboost_date_time-mt-d.so.1.41.0
(符号链接)。
A.1) -l:libboost_date_time-mt-da
==>;链接正常,即使没有库文件,二进制文件也可以工作。
A.2) -l:libboost_date_time-mt-da
与 -static
==> 链接错误 /usr/bin/ld: 找不到 -lm
C.1) -lboost_date_time-mt-d
==>链接正常,二进制文件可以工作,但需要共享库文件。
C.2) -lboost_date_time-mt-d
与 -static
==> 链接错误 /usr/bin/ld: 找不到 -lm
关于 A.2 和 C.2 中的错误有什么想法吗?
此外,在 C.1 中运行程序时,似乎搜索名为 libboost_date_time-mt-d.so.1.41.0
的共享库文件,而不是 libboost_date_time-mt-d。所以。如果程序在没有确切版本的库的系统上运行,那不是很不方便吗?使用共享库时处理版本的实用方法是什么?
Apologize because for the moment I don't have the environment to experiment and sort out the following questions myself:
1) Let's say I have four library files: libmylib_super.a
and libmylib_super.so
, mylib_dumb.a
and mylib_dumb.so
. While specifying libraries to link to, what are the differences between the following approaches:
A) -l:libmylib_super.a
B) -llibmylib_super
C) -lmylib_super
D) -lmylib_dumb
2) Definition of -static
from man page:
On systems that support dynamic
linking, this prevents linking with
the shared libraries. On other
systems, this option has no effect.
Does this linker option have anything to do with question #1? Or... by any chance will they interfere with each other?
Thanks.
--- edited 2009-12-28 ---
I just got my environment up and experimenting a bit, by linking to Boost date_time library. Say I have three library files: libboost_date_time-mt-d.a
, libboost_date_time-mt-d.so.1.41.0
, libboost_date_time-mt-d.so -> libboost_date_time-mt-d.so.1.41.0
(sym link).
A.1) -l:libboost_date_time-mt-d.a
==> linking OK, binary works even without the library file.
A.2) -l:libboost_date_time-mt-d.a
with -static
==> linking error /usr/bin/ld: cannot find -lm
C.1) -lboost_date_time-mt-d
==> linking OK, binary works but requires the shared library file.
C.2) -lboost_date_time-mt-d
with -static
==> linking error /usr/bin/ld: cannot find -lm
Any idea about the error in A.2 and C.2?
Additionally, while running the program in C.1, it seems searching the shared library file with name libboost_date_time-mt-d.so.1.41.0
but not the libboost_date_time-mt-d.so
. Wouldn't that be inconvenient if the program is running on a system without the exact version of library? What could be the practical way to handle the version while using shared library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据手册,
A)在库路径中搜索名为
libmylib_super.a
的文件(首先搜索共享库行为不适用)B)在库路径中搜索名为
libmylib_super.a
的文件首先liblibmylib_super.so
,然后liblibmylib_super.a
,或者如果-static
是,则仅搜索名为liblibmylib_super.a
的文件使用 - 请注意,链接器添加了lib
前缀和文件扩展名C) 首先在库路径中搜索名为
libmylib_super.so
的文件,然后搜索libmylib_super.so。 a
或仅在使用-static
时搜索名为liblibmylib_super.so
的文件D) 参见 C)
请注意,B) 不起作用,因为它是应将
lib
前缀添加到库名称的链接器。请注意,D) 不起作用,因为您的
mylib_dumb
不遵循命名约定。请参阅 GNU 链接器手册:
According to the manual,
A) searches the library path for a file exactly named
libmylib_super.a
(the search first for a shared library behavior doesn't apply)B) searches the library path for a file named
liblibmylib_super.so
first thenliblibmylib_super.a
or only searches for a file namedliblibmylib_super.a
if-static
is used -- note it's the linker that adds thelib
prefix and the file extensionC) searches the library path for a file named
libmylib_super.so
first thenlibmylib_super.a
or only searches for a file namedliblibmylib_super.so
if-static
is usedD) see C)
Note that B) won't work because it's the linker that should add the
lib
prefix to the library name.Note that D) won't work because your
mylib_dumb
doesn't follow the naming convention.See the GNU Linker Manual: