Sophus 编译错误

CmakeLists.txt调用Sophus的语法如下:

 FIND_PACKAGE(sophus REQUIRED)
 TARGET_LINK_LIBRARIES(${PROJECT_NAME}
     ${Sophus_LIBRARIES})

编译Sophus时出现如下错误:

../lib/libmyslam.so: undefined reference to `Sophus::SO3::SO3(double, double, double)'
../lib/libmyslam.so: undefined reference to `Sophus::SE3::operator*(Eigen::Matrix const&) const'
../lib/libmyslam.so: undefined reference to `Sophus::SE3::SE3(Sophus::SO3 const&, Eigen::Matrix const&)'
../lib/libmyslam.so: undefined reference to `Sophus::SE3::operator*(Sophus::SE3 const&) const'
../lib/libmyslam.so: undefined reference to `Sophus::SE3::log() const'
../lib/libmyslam.so: undefined reference to `Sophus::SE3::operator=(Sophus::SE3 const&)'
../lib/libmyslam.so: undefined reference to `Sophus::SE3::inverse() const'
../lib/libmyslam.so: undefined reference to `Sophus::SE3::SE3()'
../lib/libmyslam.so: undefined reference to `Sophus::SE3::SE3(Sophus::SE3 const&)'
collect2: error: ld returned 1 exit status

原因分析:
安装Sophus时,有个lib文件“libSophus.so”会出现在/usr/local/lib/libSophus.so (本人编译时最后执行了 sudo make install, 因此在系统文件夹里面有该lib文件)。 当执行FIND_PACKAGE(sophus REQUIRED)时,libSophus.so 应该被链接到 Sophus_LIBRARIES, 但cmake却没链接上(原因未知),因此出现这个错误。

解决办法:
显式将Sophus_LIBRARIES 链接到libSophus.so,CMakeLists.txt的代码如下:

FIND_PACKAGE(sophus REQUIRED)
set(Sophus_LIBRARIES libSophus.so)
TARGET_LINK_LIBRARIES(${PROJECT_NAME}
     ${Sophus_LIBRARIES})

注意: set后面是小括号”()”, 而非”{}”

你可能感兴趣的:(cmake,error,解决方法)