在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"
)

再进行编译就好了!

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐