编译高博十四讲代码遇到依赖项g2o和cholmod的坑

1. 找不到g2o库!在CMakeLists.txt中使用指令 message(STATUS "${G2O_FOUND}") 打印结果为NO。

问题描述:

CMakeLists.txt 中采用如下命令寻找g2o库。

# g2o 
find_package( G2O REQUIRED )
include_directories( ${G2O_INCLUDE_DIRS} )

科普时间:find_package() 会在模块路径中寻找FindNAME.cmake文件,再经由此文件寻找库所有文件,包括头文件,静态库(*.a),动态库(*.so),这种查找方法称为模块模式。更多详情参考此链接

由于我把g2o安装在 /usr/local/g2o 路径中,不在FindNAME.cmake的默认查找路径中,因此一直找不到。

解决方法:

在CMakeLists.txt中进行修改,如下所示,

set(CMAKE_PREFIX_PATH "/usr/local/g2o")
find_package( G2O REQUIRED )
message(STATUS "${G2O_INCLUDE_DIR}")
include_directories( ${G2O_INCLUDE_DIR} )

同时在FindG2O.cmake中,将 NO_DEFAULT_PATH 注释掉。如下所示,

FIND_PATH(G2O_INCLUDE_DIR g2o/core/base_vertex.h
  ${G2O_ROOT}/include
  $ENV{G2O_ROOT}/include
  /usr/local/include
  ...
  #NO_DEFAULT_PATH
  )

如果指定了NO_DEFAULT_PATH选项,所有NO_*选项都会被激活,导致 set(CMAKE_PREFIX_PATH "/usr/local/g2o") 设置的路径被跳过。

2. 运行make进行编译时,出现如下报错,

/usr/bin/ld: cannot find -lg2o_core
/usr/bin/ld: cannot find -lg2o_stuff
/usr/bin/ld: cannot find -lg2o_types_slam3d

问题描述:

编译执行文件时使用 target_link_libraries() 链接上述三个动态库文件,在 /usr/local/g2o/lib 中可以找到这些文件,从报错可以判断应该是找不到正确的链接路径。

解决方法:

在CMakeList.txt中添加如下命令行,设置动态库所在的绝对路径。

link_directories("/usr/local/g2o/lib")

关于在cmake中添加头文件目录,链接动态库、静态库的操作参考这个链接

3. 运行make编译代码,报错如下,

/home/gordon/kalibr_ws/devel/lib/libcholmod.a(cholmod_super_numeric.o): In function `cholmod_super_numeric':
cholmod_super_numeric.c:(.text+0xe78): undefined reference to `dsyrk_'
cholmod_super_numeric.c:(.text+0xf76): undefined reference to `dgemm_'
cholmod_super_numeric.c:(.text+0x129c): undefined reference to `dpotrf_'
cholmod_super_numeric.c:(.text+0x13a2): undefined reference to `dtrsm_'
cholmod_super_numeric.c:(.text+0x1dd6): undefined reference to `zherk_'
cholmod_super_numeric.c:(.text+0x1ed4): undefined reference to `zgemm_'
cholmod_super_numeric.c:(.text+0x2286): undefined reference to `zpotrf_'
cholmod_super_numeric.c:(.text+0x23b2): undefined reference to `ztrsm_'
cholmod_super_numeric.c:(.text+0x2dcb): undefined reference to `zherk_'
cholmod_super_numeric.c:(.text+0x2ec9): undefined reference to `zgemm_'
cholmod_super_numeric.c:(.text+0x325c): undefined reference to `zpotrf_'
cholmod_super_numeric.c:(.text+0x337c): undefined reference to `ztrsm_'
/home/gordon/kalibr_ws/devel/lib/libcholmod.a(cholmod_super_solve.o): In function `cholmod_super_lsolve':
cholmod_super_solve.c:(.text+0x548): undefined reference to `ztrsm_'
cholmod_super_solve.c:(.text+0x5f2): undefined reference to `zgemm_'
cholmod_super_solve.c:(.text+0x876): undefined reference to `dtrsm_'
cholmod_super_solve.c:(.text+0x91d): undefined reference to `dgemm_'
cholmod_super_solve.c:(.text+0xad0): undefined reference to `dtrsv_'
cholmod_super_solve.c:(.text+0xb5c): undefined reference to `dgemv_'
cholmod_super_solve.c:(.text+0xcc6): undefined reference to `ztrsv_'
cholmod_super_solve.c:(.text+0xd55): undefined reference to `zgemv_'
/home/gordon/kalibr_ws/devel/lib/libcholmod.a(cholmod_super_solve.o): In function `cholmod_super_ltsolve':
cholmod_super_solve.c:(.text+0x133b): undefined reference to `zgemm_'
cholmod_super_solve.c:(.text+0x13c9): undefined reference to `ztrsm_'
cholmod_super_solve.c:(.text+0x169a): undefined reference to `dgemm_'
cholmod_super_solve.c:(.text+0x1721): undefined reference to `dtrsm_'
cholmod_super_solve.c:(.text+0x18bf): undefined reference to `dgemv_'
cholmod_super_solve.c:(.text+0x1912): undefined reference to `dtrsv_'
cholmod_super_solve.c:(.text+0x1a9d): undefined reference to `zgemv_'
cholmod_super_solve.c:(.text+0x1af7): undefined reference to `ztrsv_'

问题描述:

由报错第一行可以判定该报错与Cholmod库有关。无法正确链接Cholmod库。

查看cmake输出内容,如下所示,

Found CHOLMOD: /home/gordon/kalibr_ws/devel/include/suitesparse 

可以确定寻找cholmod库的结果是suitesparse库(目前不清楚这两个库的关系,先将它们等同起来)。我之前在kalibr_ws中安装了suitesparse库,同时,g2o的readme.md也让我在根目录下安装了suitesparse库。想到kalibr_ws是采用catkin_build编译的,其架构跟cmake必定不一致,因此尝试换成根目录下的库。

解决方法:

在 ~/.bashrc 中去除kalibr_ws的环境变量,重启终端。可通过 echo $PATH 命令查看该环境变量是否被移除。或者,暂时将kalibr_ws中的devel文件夹暂时移除。使用 cmake 重新链接库文件,得到如下结果,

Found CHOLMOD: /usr/include/suitesparse 

至此即可编译成功。

原文地址:https://www.cnblogs.com/gdut-gordon/p/10863456.html