編譯高博十四講代碼遇到依賴項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 

至此即可編譯成功。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM