ROS工作空间中Drake集成Gurobi的构建与运行问题
ROS工作空间集成Drake与Gurobi的构建问题及解决方案
问题描述
问题1:Gurobi配置构建后测试程序缺失依赖库
使用--config=gurobi构建成功后,运行测试文件时触发以下错误:
/home/user/test_ws/devel/lib/simple_test/simple_test: error while loading shared libraries: libdrake_lcm.so: cannot open shared object file: No such file or directory
不添加--config=gurobi参数时无此问题。
问题2:重复构建时Gurobi符号链接冲突
再次执行构建时,触发Python安装脚本错误:
Traceback (most recent call last): File "/home/user/test_ws/build/drake/_bazel_user/c0701f1e1e0381b11260fb3e4da5ea58/execroot/drake/bazel-out/k8-opt/bin/install.runfiles/drake/tools/install/installer.py", line 485, in <module> main(sys.argv[1:]) File "/home/user/test_ws/build/drake/_bazel_user/c0701f1e1e0381b11260fb3e4da5ea58/execroot/drake/bazel-out/k8-opt/bin/install.runfiles/drake/tools/install/installer.py", line 478, in main exec(f'installer.{action}') File "<string>", line 1, in <module> File "/home/user/test_ws/build/drake/_bazel_user/c0701f1e1e0381b11260fb3e4da5ea58/execroot/drake/bazel-out/k8-opt/bin/install.runfiles/drake/tools/install/installer.py", line 169, in install self.copy_or_link(src, dst_full) File "/home/user/test_ws/build/drake/_bazel_user/c0701f1e1e0381b11260fb3e4da5ea58/execroot/drake/bazel-out/k8-opt/bin/install.runfiles/drake/tools/install/installer.py", line 145, in copy_or_link os.symlink(relative_link, dst) FileExistsError: [Errno 17] File exists: './libgurobi.so.9.5.2' -> '/home/user/test_ws/install/lib/libgurobi95.so'
手动删除该符号链接可临时解决,但需重复操作。
环境信息
目录结构
test_ws/ build/ devel/ ... src/ drake/ simple_test/ CMakeLists.txt package.xml test.cpp
simple_test包的CMakeLists.txt内容
cmake_minimum_required(VERSION 3.0.2) project(simple_test) execute_process(COMMAND ${CMAKE_C_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE ld_version) if ("${ld_version}" MATCHES "GNU gold") message("USING GOLD LINKER!") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags") endif() add_compile_options(-std=c++14 -g -DEIGEN_NO_DEBUG -Werror=return-type) #with debugging symbols find_package(drake REQUIRED PATHS /home/user/test_ws/src/drake/build/install) find_package(catkin) catkin_package() add_executable(simple_test test.cpp) target_link_libraries(simple_test drake::drake)
解决方案
针对问题1:库加载失败
动态适配Drake安装路径
使用--config=gurobi构建Drake时,产物会安装到install_gurobi目录而非默认的install,当前CMake硬编码路径导致链接了无Gurobi配置的Drake库。修改find_package逻辑:set(DRAKE_INSTALL_PATH "/home/user/test_ws/src/drake/build/install") if(DEFINED ENV{DRAKE_CONFIG} AND "$ENV{DRAKE_CONFIG}" STREQUAL "gurobi") set(DRAKE_INSTALL_PATH "/home/user/test_ws/src/drake/build/install_gurobi") endif() find_package(drake REQUIRED PATHS ${DRAKE_INSTALL_PATH})构建前先导出环境变量:
export DRAKE_CONFIG=gurobi修复rpath配置
当前使用的-Wl,--disable-new-dtags会关闭RUNPATH,导致运行时无法定位Drake依赖库。移除该选项,或显式添加Drake库路径到rpath:target_link_options(simple_test PRIVATE "-Wl,-rpath,${drake_INSTALL_PREFIX}/lib")临时应急方案
运行测试程序前手动导出库路径:export LD_LIBRARY_PATH=/home/user/test_ws/src/drake/build/install_gurobi/lib:$LD_LIBRARY_PATH ./devel/lib/simple_test/simple_test
针对问题2:符号链接冲突
修改Drake安装脚本逻辑
编辑drake/tools/install/installer.py中的copy_or_link函数,添加符号链接存在时的删除逻辑:def copy_or_link(self, src, dst): # 保留原有代码逻辑,新增以下判断 if os.path.exists(dst) or os.path.islink(dst): os.unlink(dst) os.symlink(relative_link, dst)CMake预构建自动清理
在simple_test的CMakeLists.txt中添加预构建步骤,自动删除冲突的符号链接:add_custom_command(TARGET simple_test PRE_BUILD COMMAND ${CMAKE_COMMAND} -E remove -f /home/user/test_ws/install/lib/libgurobi.so.9.5.2 COMMENT "Removing existing Gurobi symlink to avoid build conflict" )Bazel缓存清理
每次构建前清理Drake安装目标的缓存:bazel clean --expunge //tools/install:install
内容的提问来源于stack exchange,提问作者alvin
相关产品推荐
相关产品推荐

