[英]C++ : how to link against libA.so and not libA-X.Y.Z.so
I have a library A, that I develop. 我有一个我开发的库A。 When I deploy it on a machine, the corresponding libA.so and libA-XYZso are put in /usr/lib (XYZ being the version number).
当我在计算机上部署它时,相应的libA.so和libA-XYZso放在/ usr / lib中(XYZ是版本号)。
Now I develop a library B, which uses A. When I link B, I use the flag -lA. 现在,我开发一个使用A的库B。链接B时,使用标志-IA。 Then " ldd libB.so " gives me :
然后“ ldd libB.so ”给我:
(...)
libA-X.Y.Z.so => /usr/lib/libA-X.Y.Z.so
(...)
My problem is that when I release a new version of A (XYZZ), I also have to release a new version of B. Otherwise, someone installing the latest A won't be able to install B which will be looking for the version XYZ which doesn't exist. 我的问题是,当我发布A的新版本(XYZZ)时,我也必须发布B的新版本。否则,安装最新A的人将无法安装B,而B会寻找XYZ版本不存在。
How do I solve this problem ? 我该如何解决这个问题? How can I tell B to look for libA.so and not libA-XYZso ?
如何告诉B查找libA.so而不是libA-XYZso? Or is it wrong to do so ?
还是这样做是错误的? even unsafe ?
甚至不安全?
Update 1 : library A (that I inherited from someone else) uses autotools. 更新1 :库A(我从别人那里继承来的)使用自动工具。
Update 2 : when I build library A, I can see : "-Wl,-soname -Wl,libA-0.6.1.so" . 更新2 :构建库A时,可以看到: “ -Wl,-soname -Wl,libA-0.6.1.so” 。 If I understand properly that means that we are forcing the soname to be libA-0.6.1.so .
如果我正确理解,则意味着我们正在将soname强制为libA-0.6.1.so 。 Is that right ?
那正确吗 ? Now my problem is that I have no clue how to modify this behaviour in a project which uses autotools.
现在我的问题是我不知道如何在使用自动工具的项目中修改此行为。 I googled for a while but can't find any useful information.
我搜索了一段时间,但找不到任何有用的信息。 Should I modify configure.in or a Makefile.am ?
我应该修改configure.in还是Makefile.am吗?
When you create libA.so, pass the -soname option to the linker (if you linking through gcc, use -Wl,-soname). 创建libA.so时,将-soname选项传递给链接器(如果通过gcc进行链接,请使用-Wl,-soname)。 Then, when B gets linked, the linker refers to A through its soname, not through its filename.
然后,当B被链接时,链接器通过其soname(而不是通过其文件名)引用A。 On the target system, make sure you have a link from the soname to the real file.
在目标系统上,确保您具有从soname到真实文件的链接。 See
看到
http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html
This also works in Windows as "DLL hell" :). 这在Windows中也可以作为“ DLL hell” :)使用。
If B needs a specific version of A and you would link to libA not libA-XYZ then only substituting libA with newer version might cause B not to load or crash. 如果B需要A的特定版本,并且您将链接到libA而不是libA-XYZ,那么仅用较新版本替换libA可能会导致B无法加载或崩溃。
But of course you can do a symlink from libA-XYZ to libA-X1.Y1.Z1. 但是,当然可以从libA-XYZ到libA-X1.Y1.Z1建立符号链接。 If no APIs changed and only implementations than you should be safe.
如果没有更改任何API,并且仅实现比您应该安全。
Answering to my second update : In the Makefile.am of libA, I modified _la_LDFLAGS from -release to -avoid-version . 回答我的第二个更新:在libA的Makefile.am中,我将_la_LDFLAGS从-release修改为-avoid-version 。 This created a shared library without version number and I then recompiled libB which successfully linked against this unversioned shared library.
这创建了一个没有版本号的共享库,然后我重新编译了libB,该库成功与此未版本控制的共享库链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.