静态链接到动态库。 glibc的

时间:2022-05-16 20:09:12

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

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

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

这是由于新软件的一些依赖性问题。 (需要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.

当我将这个新软件与4.1库链接时,它链接得很好。但是,当涉及到执行软件时,它无法找到库,因为它在我的LD_LIBRARY_PATH中查看3.4.6。如果我将LD_LIBRARY_PATH设置为4.1 lib,它会破坏shell,同时杀死其他东西,因为3.4.6库用于此目的。

Its a bit of a catch 22.

它有点像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?

有没有办法在链接时我可以在不使用LD_LIBRARY_PATH的情况下给出该共享库的绝对路径?

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

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

3 个解决方案

#1


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:

你的意思是一个绝对路径,它在程序启动时使用,在寻找库时更受青睐? rpath就是这样。它将覆盖LD_LIBRARY_PATH中设置的默认搜索路径和内容。告诉gcc将其传递给链接器:

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:

#2


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.

您应该能够通过将新的lib路径添加到/etc/ld.so.conf并以root身份运行ldconfig来解决问题。

#3


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吗?即而不是将其全局设置为导出变量,将程序运行为

LD_LIBRARY_PATH=/path/to/4.1/libs my_executabel

?

-k

#1


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:

你的意思是一个绝对路径,它在程序启动时使用,在寻找库时更受青睐? rpath就是这样。它将覆盖LD_LIBRARY_PATH中设置的默认搜索路径和内容。告诉gcc将其传递给链接器:

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:

#2


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.

您应该能够通过将新的lib路径添加到/etc/ld.so.conf并以root身份运行ldconfig来解决问题。

#3


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吗?即而不是将其全局设置为导出变量,将程序运行为

LD_LIBRARY_PATH=/path/to/4.1/libs my_executabel

?

-k