gcc 链接共享库与符号链接
我有两个库,例如两个烤面包机库 libtoaster_a.so 和 libtoaster_b.so 以及所有关联的主要/次要/修订符号链接,例如 libtoaster_a.so.1.0 .0 等。两个库都实现相同的烤面包机接口,但只是进行不同的处理。因此,当我构建一个使用该库的应用程序时,使用哪个库并不重要(从应用程序的角度来看,它们是相同的)。
因为我想在应用程序编译和分发后决定使用哪个库,所以我创建了一个符号链接 libtoaster.so ,它指向 libtoaster.so.1 ,它可以然后指向 libtoaster_a.so.1 和 libtoaster_b.so.1。因此,用户/安装者可以简单地更改 libtoaster.so.1 链接来选择要使用的实现。
对于构建来说,默认情况下我将 libtoaster.so.1 链接到 libtoaster_a.so.1 。当我编译我的应用程序时,例如: my_app 通过 gcc -o my_app -ltoaster...
之类的东西编译,甚至使用 libtoaster_a.so.1 运行强>正确。但是,如果我在 my_app 上运行 ldd,我会看到它链接到 libtoaster_a.so.1 而不是所需的 libtoaster.so.1,因此更改了 libtoaster .so.1 链接无效。
有没有更好的方法来解决这个问题,而不是制作libtoaster_a.so.1,将其重命名为libtoaster.so.1,针对此库制作my_app,然后删除libtoaster .so.1 并再次将其创建为符号链接?
I have two libraries, for example two toaster libraries libtoaster_a.so and libtoaster_b.so and all the associated major/minor/rev symlinks eg libtoaster_a.so.1.0.0 etc. Both libraries implement the same toaster interface, but simply do the processing differently. Hence when I build an application that uses the library it doesn't matter which is used (from the applications perspective they are the same).
Because I would like to decide which library to use after the application has been compiled and distributed I make a symbolic link libtoaster.so which points to libtoaster.so.1 which can then point to either libtoaster_a.so.1 and libtoaster_b.so.1. Hence the user/installer could simply change the libtoaster.so.1 link to choose the implementation to use.
For the build say I have libtoaster.so.1 linked to libtoaster_a.so.1 by default. when I compile my application eg: my_app by something like gcc -o my_app -ltoaster...
it compiles and even runs with libtoaster_a.so.1 correctly. However if I run ldd on my_app I will see it is linked to libtoaster_a.so.1 rather than libtoaster.so.1 as desired, hence changing the libtoaster.so.1 link has no effect.
Is there a nicer way to fix this than making libtoaster_a.so.1, renaming it to libtoaster.so.1, making my_app against this library then deleting libtoaster.so.1 and creating it as a symbolic link again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您构建共享库时,将“-Wl,-soname=libtoaster.so.1”添加到 gcc 标志中(假设您正在与 gcc 链接)。这会在库中设置 DT_SONAME,并将强制与该库链接的任何应用程序从 DT_SONAME 获取库名称,而不是从文件名称获取。
When you build the shared libraries, add "-Wl,-soname=libtoaster.so.1" to the gcc flags (assuming you are linking with gcc). This sets DT_SONAME in the library, and will force any application linked against that library to have the name of the library taken from the DT_SONAME, rather than from the name of the file.