静态链接到动态库。 glibc

发布于 2024-07-12 09:39:07 字数 369 浏览 8 评论 0原文

所以。 我遇到一个问题,一台机器上有两个版本的 GCC。
3.4.6 和 4.1

这是由于新软件的一些依赖性问题。 (需要 glibc 4.1)

当我将这个新软件与 4.1 库链接时,它链接得很好。 但是,当执行软件时,它找不到该库,因为它正在我的 LD_LIBRARY_PATH 中查找 3.4.6。 如果我将 LD_LIBRARY_PATH 设置为 4.1 库,它会炸毁 shell,并杀死其他东西,因为 3.4.6 库用于此目的。

22.

有什么方法可以在链接时提供该共享库的绝对路径而不使用 LD_LIBRARY_PATH 吗?

这样我就有希望拥有两个版本,但对于这个特定的应用程序只使用 4.1?

So. I have a problem where I have two versions of GCC on a machine.
3.4.6 and 4.1

This is due to some dependency issues with a new piece of software. (requires glibc 4.1)

When I go to link this new software with the 4.1 libraries it links fine. However, when it comes to executing the software it can't find the library, because it is looking at 3.4.6 in my LD_LIBRARY_PATH. If I set LD_LIBRARY_PATH to the 4.1 lib it blows up the shell,among killing other things, because the 3.4.6 libraries are used for that.

Its a bit of a catch 22.

Is there any way that at link time I can give an absolute path to that shared library without using the LD_LIBRARY_PATH?

This way I can hopefully have both versions, but only use 4.1 for this specific application?

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

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

发布评论

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

评论(3

无尽的现实 2024-07-19 09:39:07

您的意思是程序启动时使用的绝对路径,并且在查找库时首选该绝对路径? rpath 就是这样。 它将覆盖默认搜索路径和 LD_LIBRARY_PATH 中设置的内容。 只需告诉 gcc 将其传递给链接器即可:

g++ -Wl,-rpath,/usr/lib/my_4.1 -omy_binary *.cpp

您可以让它向您显示搜索处理(使用 help 使其为您提供更多选项):

[js@HOST2 cpp]$ LD_DEBUG=libs ./a.out
  5859:     find library=libc.so.6 [0]; searching
  5859:      search path=/usr/lib/my_4.1/tls/i686/sse2:/usr/lib/my_4.1/tls/i686:
               /usr/lib/my_4.1/tls/sse2:/usr/lib/my_4.1/tls:
               /usr/lib/my_4.1/i686/sse2:/usr/lib/my_4.1/i686:
               /usr/lib/my_4.1/sse2:/usr/lib/my_4.1  (RPATH from file ./a.out)
  5859:       trying file=/usr/lib/my_4.1/tls/i686/sse2/libc.so.6
  5859:       ....
  5859:      search cache=/etc/ld.so.cache
  5859:       trying file=/lib/libc.so.6  (note: found here!)
  5859:

You mean an absolute path that's used when the program is started and that's favored when looking for libraries? rpath is exactly that. It will overwrite the default search path and stuff set in LD_LIBRARY_PATH. Just tell gcc to pass it through to the linker:

g++ -Wl,-rpath,/usr/lib/my_4.1 -omy_binary *.cpp

You can make it show you the search processing (use help to make it give you more options):

[js@HOST2 cpp]$ LD_DEBUG=libs ./a.out
  5859:     find library=libc.so.6 [0]; searching
  5859:      search path=/usr/lib/my_4.1/tls/i686/sse2:/usr/lib/my_4.1/tls/i686:
               /usr/lib/my_4.1/tls/sse2:/usr/lib/my_4.1/tls:
               /usr/lib/my_4.1/i686/sse2:/usr/lib/my_4.1/i686:
               /usr/lib/my_4.1/sse2:/usr/lib/my_4.1  (RPATH from file ./a.out)
  5859:       trying file=/usr/lib/my_4.1/tls/i686/sse2/libc.so.6
  5859:       ....
  5859:      search cache=/etc/ld.so.cache
  5859:       trying file=/lib/libc.so.6  (note: found here!)
  5859:
深海里的那抹蓝 2024-07-19 09:39:07

并不是您问题的真正答案,而是替代解决方案:

您应该能够通过将新的 lib 路径添加到 /etc/ld.so.conf 并运行 ldconfig 来解决您的问题 作为根用户。

not really an answer to your question, but an alternate solution:

you should be able to fix up your issues by adding your new lib path to /etc/ld.so.conf and running ldconfig as root.

我不在是我 2024-07-19 09:39:07

不能只为需要它的应用程序设置 LD_LIBRARY_PATH 吗?
即,不要将其全局设置为导出变量,而是以

LD_LIBRARY_PATH=/path/to/4.1/libs my_executabel

运行程序?

-k

Can't you set LD_LIBRARY_PATH just for the application that needs it?
I.e. instead of setting it globally as an exported variable, run your program as

LD_LIBRARY_PATH=/path/to/4.1/libs my_executabel

?

-k

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