gcc 链接共享库与符号链接

发布于 2024-11-15 10:28:31 字数 1047 浏览 2 评论 0原文

我有两个库,例如两个烤面包机库 libtoaster_a.solibtoaster_b.so 以及所有关联的主要/次要/修订符号链接,例如 libtoaster_a.so.1.0 .0 等。两个库都实现相同的烤面包机接口,但只是进行不同的处理。因此,当我构建一个使用该库的应用程序时,使用哪个库并不重要(从应用程序的角度来看,它们是相同的)。

因为我想在应用程序编译和分发后决定使用哪个库,所以我创建了一个符号链接 libtoaster.so ,它指向 libtoaster.so.1 ,它可以然后指向 libtoaster_a.so.1libtoaster_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 技术交流群。

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

发布评论

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

评论(1

云之铃。 2024-11-22 10:28:31

当您构建共享库时,将“-Wl,-soname=libtoaster.so.1”添加到 gcc 标志中(假设您正在与 gcc 链接)。这会在库中设置 DT_SONAME,并将强制与该库链接的任何应用程序从 DT_SONAME 获取库名称,而不是从文件名称获取。

[vps@manticore]~/cprog/toaster1$ gcc -c my_app.c
[vps@manticore]~/cprog/toaster1$ gcc -c toaster.c
[vps@manticore]~/cprog/toaster1$ gcc -o libtoaster_a.so -shared -Wl,-soname=libtoaster.so toaster.o
[vps@manticore]~/cprog/toaster1$ gcc -R$(pwd) -L. -ltoaster_a -o my_app my_app.o
[vps@manticore]~/cprog/toaster1$ ldd my_app
my_app:
my_app: can't load library 'libtoaster.so'
my_app: exit status 4
[vps@manticore]~/cprog/toaster1$ ln -s libtoaster_a.so libtoaster.so
[vps@manticore]~/cprog/toaster1$ ldd my_app
my_app:
    Start    End      Type Open Ref GrpRef Name
    1c000000 3c004000 exe  1    0   0      my_app
    05b1f000 25b23000 rlib 0    1   0      /home/vps/cprog/toaster1/libtoaster.so
    084f9000 28532000 rlib 0    1   0      /usr/lib/libc.so.51.0
    09e80000 09e80000 rtld 0    1   0      /usr/libexec/ld.so
[vps@manticore]~/cprog/toaster1$

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.

[vps@manticore]~/cprog/toaster1$ gcc -c my_app.c
[vps@manticore]~/cprog/toaster1$ gcc -c toaster.c
[vps@manticore]~/cprog/toaster1$ gcc -o libtoaster_a.so -shared -Wl,-soname=libtoaster.so toaster.o
[vps@manticore]~/cprog/toaster1$ gcc -R$(pwd) -L. -ltoaster_a -o my_app my_app.o
[vps@manticore]~/cprog/toaster1$ ldd my_app
my_app:
my_app: can't load library 'libtoaster.so'
my_app: exit status 4
[vps@manticore]~/cprog/toaster1$ ln -s libtoaster_a.so libtoaster.so
[vps@manticore]~/cprog/toaster1$ ldd my_app
my_app:
    Start    End      Type Open Ref GrpRef Name
    1c000000 3c004000 exe  1    0   0      my_app
    05b1f000 25b23000 rlib 0    1   0      /home/vps/cprog/toaster1/libtoaster.so
    084f9000 28532000 rlib 0    1   0      /usr/lib/libc.so.51.0
    09e80000 09e80000 rtld 0    1   0      /usr/libexec/ld.so
[vps@manticore]~/cprog/toaster1$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文