在利用cmake 进行编译的时候发生了如下所是的错误
Scanning dependencies of target usesophus
[ 50%] Building CXX object CMakeFiles/usesophus.dir/useSophus.cpp.o
[100%] Linking CXX executable usesophus
/usr/bin/ld: CMakeFiles/usesophus.dir/useSophus.cpp.o: in function `main':
/home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:17: undefined reference to `Sophus::SO3::SO3(Eigen::Matrix<double, 3, 3, 0, 3, 3> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:18: undefined reference to `Sophus::SO3::SO3(double, double, double)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:20: undefined reference to `Sophus::SO3::SO3(Eigen::Quaternion<double, 0> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:28: undefined reference to `Sophus::SO3::log() const'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:31: undefined reference to `Sophus::SO3::hat(Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:33: undefined reference to `Sophus::SO3::hat(Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:33: undefined reference to `Sophus::SO3::vee(Eigen::Matrix<double, 3, 3, 0, 3, 3> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:37: undefined reference to `Sophus::SO3::exp(Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:37: undefined reference to `Sophus::SO3::operator*(Sophus::SO3 const&) const'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:42: 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/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:43: undefined reference to `Sophus::SE3::SE3(Eigen::Quaternion<double, 0> const&, Eigen::Matrix<double, 3, 1, 0, 3, 1> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:49: undefined reference to `Sophus::SE3::log() const'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:52: undefined reference to `Sophus::SE3::hat(Eigen::Matrix<double, 6, 1, 0, 6, 1> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:54: undefined reference to `Sophus::SE3::hat(Eigen::Matrix<double, 6, 1, 0, 6, 1> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:54: undefined reference to `Sophus::SE3::vee(Eigen::Matrix<double, 4, 4, 0, 4, 4> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:59: undefined reference to `Sophus::SE3::exp(Eigen::Matrix<double, 6, 1, 0, 6, 1> const&)'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:59: undefined reference to `Sophus::SE3::operator*(Sophus::SE3 const&) const'
/usr/bin/ld: /home/zhao/learn/slamle/ownceshi/ch4/useSophus.cpp:61: undefined reference to `Sophus::SE3::matrix() const'
/usr/bin/ld: CMakeFiles/usesophus.dir/useSophus.cpp.o: in function `Sophus::operator<<(std::ostream&, Sophus::SO3 const&)':
/usr/local/include/sophus/so3.h:126: undefined reference to `Sophus::SO3::log() const'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/usesophus.dir/build.make:84:usesophus] 错误 1
make[1]: *** [CMakeFiles/Makefile2:76:CMakeFiles/usesophus.dir/all] 错误 2
make: *** [Makefile:84:all] 错误 2
所使用的CMakeLists.txt文件如下
cmake_minimum_required(VERSION 2.8)
project(use_sophus)
set(CMAKE_BUILD_TYPE “Debug” )
find_package(Sophus REQUIRED)
include_directories("/usr/include/eigen3")
include_directories(${Sophus_INCLUDE_DIRS})
add_executable(usesophus useSophus.cpp)
target_link_libraries(usesophus Sophus::Sophus)
发现安装Sophus时,有个lib文件“libSophus.so”会出现在/usr/local/lib/libSophus.so (本人编译时最后执行了 sudo make install, 因此在系统文件夹里面有该lib文件)。 当执行find_package(Sophus REQUIRED),libSophus.so 应该被链接到 Sophus_LIBRARIES, 但cmake却没链接上(原因未知),因此出现这个错误。
修改为下之后就好了
cmake_minimum_required(VERSION 2.8)
project(use_sophus)
set(CMAKE_BUILD_TYPE "Debug" )
find_package(Sophus REQUIRED)
set(Sophus_LIBRARIES "/usr/local/lib/libSophus.so")
include_directories( ${Sophus_INCLUDE_DIRS} )
include_directories("/usr/include/eigen3")
add_executable(usesophus useSophus.cpp)
target_link_libraries( usesophus ${Sophus_LIBRARIES} )