Linux 如何指定链接时使用的库版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3840218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify the library version to use at link time?
提问by Didier Trosset
Following question How do applications resolve to different versions of shared libraries at run time?, I wondered how to specify on the link command line which version of the library to use?
以下问题应用程序如何在运行时解析不同版本的共享库?,我想知道如何在链接命令行上指定使用哪个版本的库?
Let's say I have
假设我有
libmy.so.1.0
libmy.so.1 -> libmy.so.1.0
libmy.so.2.0
libmy.so.2 -> libmy.so.2.0
libmy.so -> libmy.so.2
The usual way to specify the library to link with the executable does not show the version to use. Furthermore, it is very likely that one wants to link with the most recent version. Thus the usual line works fine in most cases.
指定要与可执行文件链接的库的常用方法不显示要使用的版本。此外,很可能希望与最新版本链接。因此,通常的线路在大多数情况下都可以正常工作。
gcc app.o -lmy -o app
What is the command line to link app
that should use version 1 of the library?
app
应该使用库版本 1 的链接命令行是什么?
采纳答案by Dmitry Yudakov
The linker is able to accept filenames too
链接器也可以接受文件名
gcc app.o -l:libmy.so.1 -o app
From man ld
:
来自man ld
:
-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.
-l namespec
--library=namespec
将 namespec 指定的存档或目标文件添加到要链接的文件列表中。此选项可以使用任意次数。 如果 namespec 是 :filename 形式,ld 将在库路径中搜索名为 filename 的文件,否则它将在库路径中搜索名为 libnamespec.a 的文件。
I noticed that older versions do not support it, so check man ld
-l
or --library
option on your system.
我注意到旧版本不支持它,因此请检查man ld
-l
或--library
选择您的系统。
You could also link to the file mentioning its full name
您还可以链接到提及其全名的文件
gcc app.o /mylibpath/libmy.so.1 -o app