Ubuntu下编译matlab eigen时: undefined reference to `engOpen'

在Ubuntu系统中尝试编译包含MATLAB eigen的项目时,遇到`undefined reference to `engOpen'`错误。问题源于链接阶段未能正确引用libeng.so库。确保在编译命令中使用 `-leng` 而不是 `-llibeng`,因为Linux系统不需要`lib`前缀。同时,要注意MATLAB库在Windows和Linux之间的不兼容性,避免混淆库文件。正确的编译命令应为:`g++ matlab_eigen.cpp -o matlab_eigen -I/opt/MATLAB/R2012a/extern/include -L/opt/MATLAB/R2012a/bin/glnxa64 -leng -lmx`。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Ubuntu中,c++调用matlab引擎( 入门实例点这里),已经设置好了库文件的查找路径,但是编译时还是报错:
$ g++ matlab_eigen.cpp -o matlab_eigen -I/opt/MATLAB/R2012a/extern/include -L/opt/MATLAB/R2012a/bin/glnxa64
/tmp/cccXS6eb.o: In function `main':
matlab_eigen.cpp:(.text+0xe): undefined reference to `engOpen'
matlab_eigen.cpp:(.text+0x52): undefined reference to `engEvalString'
matlab_eigen.cpp:(.text+0x63): undefined reference to `engEvalString'
matlab_eigen.cpp:(.text+0x74): undefined reference to `engEvalString'
matlab_eigen.cpp:(.text+0x85): undefined reference to `engEvalString'
collect2: ld returned 1 exit status

原因:缺少需要的链接库:
在windows下:libeng.lib libmx.lib libmex.lib libmat.lib (依次对应
/usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::exp(Eigen::Matrix<double, 6, 1, 0, 6, 1> const&)' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::operator=(Sophus::SE3 const&)' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::SE3()' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::SE3(Sophus::SE3 const&)' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::operator*(Sophus::SE3 const&) const' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SO3::matrix() const' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::SE3(Eigen::Matrix<double, 3, 3, 0, 3, 3> const&, Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::operator*(Eigen::Matrix<double, 3, 1, 0, 3, 1> const&) const' collect2: error: ld returned 1 exit status make[2]: *** [rpg_vikit/vikit_common/CMakeFiles/test_vk_common_camera.dir/build.make:134: /home/dog/catkin_ws/devel/lib/vikit_common/test_vk_common_camera] Error 1 make[1]: *** [CMakeFiles/Makefile2:575: rpg_vikit/vikit_common/CMakeFiles/test_vk_common_camera.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs.... /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::exp(Eigen::Matrix<double, 6, 1, 0, 6, 1> const&)' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::operator=(Sophus::SE3 const&)' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::SE3()' /usr/bin/ld: /home/dog/catkin_ws/devel/lib/libvikit_common.so: undefined reference to `Sophus::SE3::SE3(Sophus::SE3 const&)'
最新发布
04-03
<think>嗯,用户遇到了编译错误,是关于Sophus中SE3类的未定义引用问题。具体报错是“undefined reference to `Sophus::SE3::operator*...”。我需要帮他解决这个问题。首先,我应该回忆一下常见的导致未定义引用的原因,通常是链接器找不到相应的文件或者头文件路径不正确。 根据用户提供的引用,特别是引用[1],里面提到需要在CMakeLists.txt中显式链接到libSophus.so。用户可能没有正确配置CMakeLists.txt文件,导致编译器找不到Sophus。可能的问题点包括:没有调用find_package(Sophus REQUIRED)、没有正确设置包含目录,或者没有将目标链接到Sophus。 另外,引用[3]提到需要检查include_directories是否包含Sophus的头文件路径。如果用户的项目没有正确包含Sophus的安装路径,那么头文件可能无法找到,导致编译错误。同,引用[4]指出可能需要设置编译器标志来避免某些警告被视为错误,尤其是在较新版本的Sophus中可能存在的兼容性问题。 接下来,我需要逐步指导用户检查他们的CMake配置。第一步应该是确认find_package(Sophus REQUIRED)被正确调用,并且Sophus的路径被正确设置。然后检查include_directories是否包含Sophus的头文件目录,如${Sophus_INCLUDE_DIRS}。接着,确保target_link_libraries中链接了Sophus的文件,可能需要使用${Sophus_LIBRARIES}。 此外,用户可能需要处理版本兼容性问题,比如Sophus的模板化版本和非模板化版本之间的差异。如果用户使用的是较新的模板化版本,可能需要包含不同的头文件路径,例如Sophus/sophus而不是Sophus。同,确保编译器标志正确,如引用[4]提到的添加-Wno-deprecated-declarations来避免某些警告导致编译失败。 最后,建议用户清理构建目录并重新编译,有候旧的构建文件可能导致问题。如果所有配置都正确,问题可能出在Sophus的安装是否正确,或者项目结构中的路径是否正确引用。</think>### 编译错误解决方案:Sophus中SE3类未定义引用 #### 问题分析 该错误属于**链接器错误**,表示编译器找到了函数声明(头文件),但找不到对应的实现(文件)。常见原因包括: 1. 头文件路径未正确包含 2. 文件未正确链接 3. Sophus版本不兼容(模板化版本 vs 非模板化版本) 4. 编译器参数缺失 --- #### 分步解决方法 **1. 确保CMakeLists配置正确** ```cmake # 必须包含Sophus的查找 find_package(Sophus REQUIRED) # 包含头文件目录(注意不同版本路径差异) include_directories( ${Sophus_INCLUDE_DIRS} # 如果是模板化版本可能需要添加: # /usr/local/include/sophus ) # 显式链接文件 target_link_libraries(${PROJECT_NAME} ${Sophus_LIBRARIES} # 某些系统需要直接指定: # Sophus::Sophus ) ``` ▸ **验证点**:检查`SophusConfig.cmake`文件是否存在于`/usr/local/lib/cmake/Sophus/`[^1] **2. 版本兼容性处理** ```cmake # 模板化版本需要特殊处理 set(Sophus_USE_BASIC_IMPLICIT_CONVERSIONS ON) add_compile_options(-DUSE_SOPHUS_TEMPLATE) ``` ▸ **注意**:模板化版本(github最新版)与非模板化版本(ROS自带版)API存在差异[^4] **3. 编译器参数配置** ```cmake # 避免deprecated警告导致编译失败 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations") ``` --- #### 典型错误配置对比 | 错误配置 | 正确配置 | |---------|---------| | `include_directories(/usr/include/Sophus)` | `include_directories(${Sophus_INCLUDE_DIRS})` | | `target_link_libraries(my_target Sophus)` | `target_link_libraries(my_target ${Sophus_LIBRARIES})` | | 缺少`find_package(Sophus)` | 必须显式调用find_package | --- #### 补充验证步骤 1. 执行`catkin_make -DCMAKE_VERBOSE_MAKEFILE=ON`查看详细链接过程 2. 检查`libSophus.so`是否存在于`/usr/local/lib/` 3. 确认代码中正确包含头文件: ```cpp #include <sophus/se3.hpp> // 模板化版本 // 或 #include <Sophus/SE3.h> // 非模板化版本 ``` --- #### 引用说明 [^1]: 显式链接文件是解决未定义引用的关键步骤 [^3]: 头文件包含路径必须与实际安装位置一致 : 编译器参数设置可避免兼容性警告导致的构建失败 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值