[英]Load shared libraries from lib directory in CMake?
I have downloaded a ROS2 demo from the examples repository .我已经从示例存储库下载了一个 ROS2 演示。
Specifically, I have used minimal_subscriber and minimal_publisher .具体来说,我使用了minimum_subscriber和minimum_publisher 。
I have ROS2 installed in /opt/ros2
and when I build these two examples with colcon build
, it generates an install/
directory with lib/
, shared/
and the other usual directory structure.我将 ROS2 安装在/opt/ros2
,当我使用colcon build
构建这两个示例时,它会生成一个install/
目录,其中包含lib/
、 shared/
和其他常用目录结构。
I can execute and run these demos perfectly fine with my current setup in my machine, but these executables link to libraries present in /opt/ros2
, so when I want to execute them in another machine without ROS2 installed or I move my ROS2 installation in my machine, the executables cannot find the shared objects.我可以在我的机器上用我当前的设置完美地执行和运行这些演示,但是这些可执行文件链接到/opt/ros2
存在的库,所以当我想在没有安装 ROS2 的另一台机器上执行它们时,或者我将我的 ROS2 安装移动到我的机器,可执行文件找不到共享对象。
I've added a really simple script that adds all dependencies to install/lib
when building but the executables don't seem to care, they aren't looking for shared libraries in the generated lib
directory, they keep trying to search in /opt/ros2
.我添加了一个非常简单的脚本,在构建时将所有依赖项添加到install/lib
但可执行文件似乎并不关心,他们没有在生成的lib
目录中寻找共享库,他们一直试图在/opt/ros2
搜索/opt/ros2
。
I believe this is something I should solve in CMake and it's not ROS2 specific, so, is there any way I can tell my generated executables to search in a diferent directory?我相信这是我应该在 CMake 中解决的问题,它不是特定于 ROS2 的,所以,有什么方法可以告诉我生成的可执行文件在不同的目录中搜索吗? ( ../lib
or ./lib
in my case) ( ../lib
或./lib
在我的情况下)
If you are building them yourself (assumed since you mention CMake), you can set the RPATH in CMake ( docs here ).如果你自己构建它们(假设你提到了 CMake),你可以在 CMake 中设置 RPATH( 这里的文档)。 Specifically set the CMAKE_INSTALL_RPATH
something like:具体设置CMAKE_INSTALL_RPATH
类似:
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
If you can't rebuild them, you can set LD_LIBRARY_PATH
in your environment to where the libraries are located, or you can patch the executables themselves with an updated RPATH by using patchelf .如果您无法重建它们,您可以在您的环境中将LD_LIBRARY_PATH
设置为库所在的位置,或者您可以使用patchelf使用更新的 RPATH 修补可执行文件本身。
In order to get a relative RPATH rather than an absolute RPATH, use the $ORIGIN
variable in your rpath spec.为了获得相对 RPATH 而不是绝对 RPATH,请在 rpath 规范中使用$ORIGIN
变量。 See " Recommendations " the the link above for more details.有关更多详细信息,请参阅上面链接中的“ 建议”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.