如何指定链接时使用的库版本?

发布于 2024-09-25 17:07:00 字数 534 浏览 0 评论 0原文

以下问题应用程序如何解析运行时使用不同版本的共享库?,我想知道如何在链接命令行上指定使用哪个版本的库?

假设我有

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

指定与可执行文件链接的库的常用方法,但不显示要使用的版本。此外,人们很可能想要链接到最新版本。因此,在大多数情况下,通常的线路工作得很好。

gcc app.o -lmy -o app

链接应使用库版本 1 的 app 的命令行是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

灰色世界里的红玫瑰 2024-10-02 17:07:00

链接器也能够接受

gcc  app.o -l:libmy.so.1 -o app

来自 man ld 的文件名:

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

我注意到旧版本不支持它,因此请检查系统上的 man ld -l--library 选项。

您还可以链接到提及其全名的文件

gcc  app.o /mylibpath/libmy.so.1 -o app

The linker is able to accept filenames too

gcc  app.o -l:libmy.so.1 -o app

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

I noticed that older versions do not support it, so check man ld -l or --library option on your system.

You could also link to the file mentioning its full name

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