1. 程式人生 > >於g2o新版本編譯出錯的原因及解決辦法

於g2o新版本編譯出錯的原因及解決辦法

在githubg2o的github地址上面down了最新的版本進行安裝,
編譯十四講第六講的程式碼出錯,
報錯資訊:
/home/hri/SLAM/slambook/ch6/g2o_curve_fitting/main.cpp: In function ‘int main(int, char**)’:
/home/hri/SLAM/slambook/ch6/g2o_curve_fitting/main.cpp:77:49: error: no matching function for call to ‘g2o::BlockSolver<g2o::BlockSolverTraits<3, 1> >::BlockSolver(g2o::BlockSolver<g2o::BlockSolverTraits<3, 1> >::LinearSolverType*&)’
Block* solver_ptr = new Block( linearSolver ); // 矩陣塊求解器
^
In file included from /usr/local/include/g2o/core/block_solver.h:199:0,
from /home/hri/SLAM/slambook/ch6/g2o_curve_fitting/main.cpp:4:
/usr/local/include/g2o/core/block_solver.hpp:40:1: note: candidate: g2o::BlockSolver<Traits>::BlockSolver(std::unique_ptr<typename Traits::LinearSolverType>) [with Traits = g2o::BlockSolverTraits<3, 1>; typename Traits::LinearSolverType = g2o::LinearSolver<Eigen::Matrix<double, 3, 3> >]
BlockSolver<Traits>::BlockSolver(std::unique_ptr<LinearSolverType> linearSolver)
^
/usr/local/include/g2o/core/block_solver.hpp:40:1: note: no known conversion for argument 1 from ‘g2o::BlockSolver<g2o::BlockSolverTraits<3, 1> >::LinearSolverType* {aka g2o::LinearSolver<Eigen::Matrix<double, 3, 3> >*}’ to ‘std::unique_ptr<g2o::LinearSolver<Eigen::Matrix<double, 3, 3> >, std::default_delete<g2o::LinearSolver<Eigen::Matrix<double, 3, 3> > > >’
/home/hri/SLAM/slambook/ch6/g2o_curve_fitting/main.cpp:79:103: error: no matching function for call to ‘g2o::OptimizationAlgorithmLevenberg::OptimizationAlgorithmLevenberg(Block*&)’
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg( solver_ptr );
^
In file included from /home/hri/SLAM/slambook/ch6/g2o_curve_fitting/main.cpp:5:0:
/usr/local/include/g2o/core/optimization_algorithm_levenberg.h:47:16: note: candidate: g2o::OptimizationAlgorithmLevenberg::OptimizationAlgorithmLevenberg(std::unique_ptr<g2o::Solver>)
explicit OptimizationAlgorithmLevenberg(std::unique_ptr<Solver> solver);
^
/usr/local/include/g2o/core/optimization_algorithm_levenberg.h:47:16: note: no known conversion for argument 1 from ‘Block* {aka g2o::BlockSolver<g2o::BlockSolverTraits<3, 1> >*}’ to ‘std::unique_ptr<g2o::Solver>’
CMakeFiles/curve_fitting.dir/build.make:62: recipe for target 'CMakeFiles/curve_fitting.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/curve_fitting.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/curve_fitting.dir/all' failed
make[1]: *** [CMakeFiles/curve_fitting.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

出錯原因:

g2o新版本和舊版本在智慧指標上出現了不相容的問題。

解決辦法:

方法一

刪掉/解除安裝掉新版本的g2o,安裝新的SLAM十四講程式碼中附帶的第三方庫資料夾下的舊版本;

方法二

參考這篇部落格解決,需要對原始碼進行改動,替換一些指標,換成std::unique_ptr
具體的做法:參考一下幾篇博文
(1)g2o官方github的issues
其實就是解釋了出錯的原因;
(2)具體實現方法
(3)和(2)差不多
其實(2)和(3)就是一個東西,但是還是可以對照這著看一下的,後者借鑑前者的。

恩,最後最後我閒改程式碼麻煩,而且中午肚子餓了,選擇了方法一,重灌了g2o,成功!

上面的問題算是解決了,但是當我編譯的時候又會遇到這樣的問題:

CMake Error at CMakeLists.txt:11 (find_package):
By not providing "FindG2O.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "G2O", but
CMake did not find one.

Could not find a package configuration file provided by "G2O" with any of
the following names:
G2OConfig.cmake
g2o-config.cmake


Add the installation prefix of "G2O" to CMAKE_PREFIX_PATH or set "G2O_DIR"
to a directory containing one of the above files. If "G2O" provides a
separate development package or SDK, be sure it has been installed.

-- Configuring incomplete, errors occurred!
See also "/home/hri/SLAM/slambook/ch6/g2o_curve_fitting/build/CMakeFiles/CMakeOutput.log

真的是生氣,然後就搜尋解決辦法,這裡不得不說一下,百度搜索真是垃圾,搜到的都是沒用的
然後谷歌搜到第一個就解決可問題!

解決辦法如下,主要是CMakeLists.txt的書寫上要加幾行程式碼
貼上我的:
本來是這樣的就會報錯,
list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules )
find_package(G2O REQUIRED)
include_directories(
${G2O_INCLUDE_DIRS}
"/usr/include/eigen3"
)

修改為:

list( APPEND CMAKE_MODULE_PATH /home/hri/ThirdParty/g2o/cmake_modules )
set(G2O_ROOT /usr/local/include/g2o)
find_package(G2O REQUIRED)
include_directories(
${G2O_INCLUDE_DIRS}
"/usr/include/eigen3"
)

再進行編譯就好了!