单个主机上的多个 glibc 库 [英] Multiple glibc libraries on a single host
问题描述
我的 linux (SLES-8) 服务器目前有 glibc-2.2.5-235,但我有一个程序不能在这个版本上运行并且需要 glibc-2.3.3.
My linux (SLES-8) server currently has glibc-2.2.5-235, but I have a program which won't work on this version and requires glibc-2.3.3.
是否可以在同一主机上安装多个 glibcs?
Is it possible to have multiple glibcs installed on the same host?
这是我在旧的 glibc 上运行我的程序时得到的错误:
This is the error I get when I run my program on the old glibc:
./myapp: /lib/i686/libc.so.6: version `GLIBC_2.3' not found (required by ./myapp)
./myapp: /lib/i686/libpthread.so.0: version `GLIBC_2.3.2' not found (required by ./myapp)
./myapp: /lib/i686/libc.so.6: version `GLIBC_2.3' not found (required by ./libxerces-c.so.27)
./myapp: /lib/ld-linux.so.2: version `GLIBC_2.3' not found (required by ./libstdc++.so.6)
./myapp: /lib/i686/libc.so.6: version `GLIBC_2.3' not found (required by ./libstdc++.so.6)
所以我创建了一个名为 newglibc 的新目录并将以下文件复制到:
So I created a new directory called newglibc and copied the following files in:
libpthread.so.0
libm.so.6
libc.so.6
ld-2.3.3.so
ld-linux.so.2 -> ld-2.3.3.so
和
export LD_LIBRARY_PATH=newglibc:$LD_LIBRARY_PATH
但我收到一个错误:
./myapp: /lib/ld-linux.so.2: version `GLIBC_PRIVATE' not found (required by ./newglibc/libpthread.so.0)
./myapp: /lib/ld-linux.so.2: version `GLIBC_2.3' not found (required by libstdc++.so.6)
./myapp: /lib/ld-linux.so.2: version `GLIBC_PRIVATE' not found (required by ./newglibc/libm.so.6)
./myapp: /lib/ld-linux.so.2: version `GLIBC_2.3' not found (required by ./newglibc/libc.so.6)
./myapp: /lib/ld-linux.so.2: version `GLIBC_PRIVATE' not found (required by ./newglibc/libc.so.6)
所以看起来它们仍然链接到 /lib
而不是从我放置它们的地方提取.
So it appears that they are still linking to /lib
and not picking up from where I put them.
推荐答案
很可能在同一个系统上有多个版本的 glibc(我们每天都这样做).
It is very possible to have multiple versions of glibc on the same system (we do that every day).
但是,您需要知道 glibc 由许多部分(200 多个共享库)组成,所有部分都必须匹配.其中之一是 ld-linux.so.2,它必须匹配 libc.so.6,否则你会看到你看到的错误.
However, you need to know that glibc consists of many pieces (200+ shared libraries) which all must match. One of the pieces is ld-linux.so.2, and it must match libc.so.6, or you'll see the errors you are seeing.
ld-linux.so.2 的绝对路径在链接时被硬编码到可执行文件中,并且在链接完成后不能轻易更改(更新:可以使用 patchelf;参见下面的这个答案).
The absolute path to ld-linux.so.2 is hard-coded into the executable at link time, and can not be easily changed after the link is done (Update: can be done with patchelf; see this answer below).
要构建可与新 glibc 一起使用的可执行文件,请执行以下操作:
To build an executable that will work with the new glibc, do this:
g++ main.o -o myapp ...
-Wl,--rpath=/path/to/newglibc
-Wl,--dynamic-linker=/path/to/newglibc/ld-linux.so.2
-rpath
链接器选项将使运行时加载程序在 /path/to/newglibc
中搜索库(因此您不必设置 LD_LIBRARY_PATH
在运行它之前),并且 -dynamic-linker
选项将烘焙"将 ld-linux.so.2
更正到应用程序中的路径.
The -rpath
linker option will make the runtime loader search for libraries in /path/to/newglibc
(so you wouldn't have to set LD_LIBRARY_PATH
before running it), and the -dynamic-linker
option will "bake" path to correct ld-linux.so.2
into the application.
如果您无法重新链接 myapp
应用程序(例如,因为它是第三方二进制文件),并非所有内容都会丢失,但会变得更加棘手.一种解决方案是为其设置适当的 chroot
环境.另一种可能性是使用 rtldi 和一个 二进制编辑器.更新:或者您可以使用 patchelf.
If you can't relink the myapp
application (e.g. because it is a third-party binary), not all is lost, but it gets trickier. One solution is to set a proper chroot
environment for it. Another possibility is to use rtldi and a binary editor. Update: or you can use patchelf.
这篇关于单个主机上的多个 glibc 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!