本文是在改动slam14讲ch9中的g2o部分代码衍生的问题。博主本人已经完整安装了g2o库,因此不采用原书代码中的cmake文件中的库。
问题1:
/usr/local/include/g2o/solvers/csparse/csparse_extension.h:27:10: fatal error: cs.h: 没有那个文件或目录
#include <cs.h>
^~~~~~
compilation terminated.
make[3]: *** [CMakeFiles/exm1.dir/bundle_adjustment_g2o.cpp.o] Error 1
make[2]: *** [CMakeFiles/exm1.dir/all] Error 2
make[1]: *** [CMakeFiles/exm1.dir/rule] Error 2
make: *** [exm1] Error 2
问题1解决方法:
问题是在给定的文件中并没有找到 #include <cs.h>。
此时我们打开终端 ,输入:
sudo apt-get install libsuitesparse-dev
如果未安装,会自行安装CSparse.如果已经安装,会提示已经是最新版本。
如果已经安装完成,对suitesparse进行定位。
终端输入
locate suitesparse
会出现定位结果:
可见,suitesparse中包含了cs.h文件。因此,我们只需要在CMAKELists中加入路径:
include_directories("/usr/include/suitesparse")
可以解决该问题。
问题2:
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "FindCSparse.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "CSparse", but
CMake did not find one.
Could not find a package configuration file provided by "CSparse" with any
of the following names:
CSparseConfig.cmake
csparse-config.cmake
Add the installation prefix of "CSparse" to CMAKE_PREFIX_PATH or set
"CSparse_DIR" to a directory containing one of the above files. If
"CSparse" provides a separate development package or SDK, be sure it has
been installed.
问题2解决办法:
如果不设定编译路径,给定库文件,即使用原书给出的cmake文件的话,是没法通过 find_package的方法找到csparse的。
可以将代码
SET(G2O_LIBS g2o_csparse_extension g2o_stuff g2o_core cxsparse)
修改为:
SET(G2O_LIBS "/安装路径/libg2o_csparse_extension.so" "/安装路径/libg2o_stuff.so" "/安装路径/libg2o_core.so" cxsparse)
这里的安装路径亦可以通过定位获得:
locate libxxx.so
如博主改正后可以得到:
SET(G2O_LIBS "/usr/local/lib/libg2o_csparse_extension.so" "/usr/local/lib/libg2o_stuff.so" "/usr/local/lib/libg2o_core.so" cxsparse)
最终的CMakeLists如下:
cmake_minimum_required(VERSION 3.17)
project(exm1)
set(CMAKE_CXX_STANDARD 14)
Find_Package(g2o REQUIRED)
FIND_PACKAGE(Ceres REQUIRED)
FIND_PACKAGE(fmt)
include_directories("/usr/include/eigen3")
include_directories("/usr/include/suitesparse") #cs.h文件的地址
INCLUDE_DIRECTORIES(${g20_INCLUDE_DIRS} ${Sophus_INLCUDE_DIRS}${Ceres_INCLUDE_DIRS})
SET(G2O_LIBS "/usr/local/lib/libg2o_csparse_extension.so" "/usr/local/lib/libg2o_stuff.so" "/usr/local/lib/libg2o_core.so" cxsparse)
add_library(bal_common common.cpp)
add_executable(exm1 bundle_adjustment_g2o.cpp)
target_link_libraries(exm1 fmt::fmt ${G2O_LIBS} ${Sophus_INCLUDE_LIBRARIES} bal_common)