关于 GCC 链接器的问题

发布于 2024-08-16 16:04:20 字数 1461 浏览 5 评论 0原文

抱歉,因为目前我没有环境来自己实验和整理以下问题:

1)假设我有四个库文件: libmylib_super.alibmylib_super.so< /code>、mylib_dumb.amylib_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-dalibboost_date_time-mt-d.s​​o.1.41.0libboost_date_time-mt-d.s​​o - > libboost_date_time-mt-d.s​​o.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.s​​o.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪心 2024-08-23 16:04:20

根据手册,

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 链接器手册

-l 名称规范

--library=namespec

将 namespec 指定的归档文件或目标文件添加到要链接的文件列表中。该选项可以使用任意多次。如果 namespec 的形式为 :filename,ld 将在库路径中搜索名为 filename 的文件,否则它将在库路径中搜索名为 libnamespec.a 的文件。

在支持共享库的系统上,ld 还可以搜索 libnamespec.a 以外的文件。具体来说,在 ELF 和 SunOS 系统上,ld 将在目录中搜索名为 libnamespec.so 的库,然后再搜索名为 libnamespec.a 的库。 (按照惯例,.so 扩展名表示共享库。)请注意,此行为不适用于 :filename,它始终指定一个名为 filename 的文件。

链接器只会在命令行指定的位置搜索存档一次。如果存档定义了一个在命令行上出现在存档之前的某个对象中未定义的符号,则链接器将包含存档中的相应文件。但是,稍后出现在命令行上的对象中的未定义符号不会导致链接器再次搜索存档。

请参阅 -( 选项,了解强制链接器多次搜索档案的方法。

您可以在命令行上多次列出相同的存档。

这种类型的档案搜索是 Unix 链接器的标准。但是,如果您在 AIX 上使用 ld,请注意它与 AIX 链接器的行为不同。

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 then liblibmylib_super.a or only searches for a file named liblibmylib_super.a if -static is used -- note it's the linker that adds the lib prefix and the file extension

C) searches the library path for a file named libmylib_super.so first then libmylib_super.a or only searches for a file named liblibmylib_super.so if -static is used

D) 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:

-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. 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.

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 called libnamespec.so before searching for one called 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.

The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

See the -( option for a way to force the linker to search archives multiple times.

You may list the same archive multiple times on the command line.

This type of archive searching is standard for Unix linkers. However, if you are using ld on AIX, note that it is different from the behaviour of the AIX linker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文