Linux C++ 尝试使用绝对路径加载一个特定的库,而所有其他库都使用相对路径进行链接
Linux C++ trys to load one specific library using an absolute path, while all otheres are linked using a relative one
我遇到以下问题:我正在尝试创建程序的可移植版本,所以我将 rpath 设置为“.”所以所有库都使用相对文件路径链接。这对除一个图书馆以外的所有图书馆都有效。出于某种原因,该程序只有在一个特定的库出现在编译时链接的相同位置时才有效。这是我自己写的,它的 rpath 也设置为“.”。所以基本上,即使库与可执行文件位于完全相同的位置,程序也会拒绝启动。
我已经确认只有一个库有问题,因为如果我在测试计算机上创建我计算机上的库所在的文件夹,程序就会启动。
linux-vdso.so.1 => (0x00007ffcc5961000)
libOgreHlmsPbs.so.2.1.0 => ./libOgreHlmsPbs.so.2.1.0 (0x00007fedeec3f000)
libOgreHlmsUnlit.so.2.1.0 => ./libOgreHlmsUnlit.so.2.1.0 (0x00007fedeea1d000)
libOgreMain.so.2.1.0 => ./libOgreMain.so.2.1.0 (0x00007fedee194000)
/home/marvin/workspace/HLMS_DS_DEMO/libHLMS_DS.so => not found
所以有没有人知道什么会导致 linux 试图在原始位置而不是像其他所有位置一样在相关位置找到图书馆?此外,该程序在 windows.
上运行良好
I set rpath to .
so all libraries are linked using the relative file path
在 rpath 中使用 .
是个糟糕的主意:
- 可用性:应用程序必须 运行 来自特定的工作目录。
- 安全性:攻击者可能会将修改后的
.so
文件放在另一个目录中,并从那里 运行 您的应用程序。
正确的方法是使用-rpath=$ORIGIN
功能。见 man ld.so
:
$ORIGIN (or equivalently ${ORIGIN})
This expands to the directory containing the program or shared object. Thus, an application located in somedir/app could be compiled with
gcc -Wl,-rpath,'$ORIGIN/../lib'
so that it finds an associated shared object in somedir/lib no matter where somedir is located in the directory hierarchy. This facilitates the creation of "turn-key" applications that do not need to be installed into special directories, but can instead be unpacked into any directory and still find their own shared objects.
$ORIGIN
语法有点不幸,因为它被 make
和 bash
作为变量扩展,因此您可能需要适当地引用它。
what could lead to linux trying to find the library at the original location instead of at the relative one like all the others
链接时,库可以指定为-lmylib
或-l:libmylib.so
或-l<path>/libmylib.so
。在后一种情况下,运行time 链接器仅在特定路径 <path>/libmylib.so
中查找库。有关完整详细信息,请参阅 man ld
、选项 -l
。您可能想查看您的构建系统链接器命令。
我遇到以下问题:我正在尝试创建程序的可移植版本,所以我将 rpath 设置为“.”所以所有库都使用相对文件路径链接。这对除一个图书馆以外的所有图书馆都有效。出于某种原因,该程序只有在一个特定的库出现在编译时链接的相同位置时才有效。这是我自己写的,它的 rpath 也设置为“.”。所以基本上,即使库与可执行文件位于完全相同的位置,程序也会拒绝启动。
我已经确认只有一个库有问题,因为如果我在测试计算机上创建我计算机上的库所在的文件夹,程序就会启动。
linux-vdso.so.1 => (0x00007ffcc5961000)
libOgreHlmsPbs.so.2.1.0 => ./libOgreHlmsPbs.so.2.1.0 (0x00007fedeec3f000)
libOgreHlmsUnlit.so.2.1.0 => ./libOgreHlmsUnlit.so.2.1.0 (0x00007fedeea1d000)
libOgreMain.so.2.1.0 => ./libOgreMain.so.2.1.0 (0x00007fedee194000)
/home/marvin/workspace/HLMS_DS_DEMO/libHLMS_DS.so => not found
所以有没有人知道什么会导致 linux 试图在原始位置而不是像其他所有位置一样在相关位置找到图书馆?此外,该程序在 windows.
上运行良好I set rpath to
.
so all libraries are linked using the relative file path
在 rpath 中使用 .
是个糟糕的主意:
- 可用性:应用程序必须 运行 来自特定的工作目录。
- 安全性:攻击者可能会将修改后的
.so
文件放在另一个目录中,并从那里 运行 您的应用程序。
正确的方法是使用-rpath=$ORIGIN
功能。见 man ld.so
:
$ORIGIN (or equivalently ${ORIGIN}) This expands to the directory containing the program or shared object. Thus, an application located in somedir/app could be compiled with
gcc -Wl,-rpath,'$ORIGIN/../lib'
so that it finds an associated shared object in somedir/lib no matter where somedir is located in the directory hierarchy. This facilitates the creation of "turn-key" applications that do not need to be installed into special directories, but can instead be unpacked into any directory and still find their own shared objects.
$ORIGIN
语法有点不幸,因为它被 make
和 bash
作为变量扩展,因此您可能需要适当地引用它。
what could lead to linux trying to find the library at the original location instead of at the relative one like all the others
链接时,库可以指定为-lmylib
或-l:libmylib.so
或-l<path>/libmylib.so
。在后一种情况下,运行time 链接器仅在特定路径 <path>/libmylib.so
中查找库。有关完整详细信息,请参阅 man ld
、选项 -l
。您可能想查看您的构建系统链接器命令。