关于G2O 的使用
1. 使用方法
使用中间文件查找对应的库文件:FindG2O.cmake文件查找
整个单个文件项目下的项目组织架构
wpf@wpfpc:~/SLAM/review_slam2/cpp_test1/chapt1$ tree -L 2
.
├── cmake-build-debug
│ ├── ch1
│ ├── ch1.cbp
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── Makefile
│ ├── Project.cbp
│ └── Testing
├── CMakeLists.txt
├── cmake_modules
│ └── FindG2O.cmake
└── curve_fitting.cpp
4 directories, 9 files
重点是
├── CMakeLists.txt
├── cmake_modules
│ └── FindG2O.cmake
└── curve_fitting.cpp
2. CMakeList.txt文件
cmake_minimum_required( VERSION 3.2 )
project( ch1 )
set( CMAKE_BUILD_TYPE "Release" )
set( CMAKE_CXX_FLAGS "-std=c++11 -O3" )
# 添加cmake模块以使用ceres库
list( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules )
# 寻找G2O
find_package( G2O REQUIRED )
include_directories(
${G2O_INCLUDE_DIRS}
"/usr/include/eigen3"
)
# OpenCV
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_DIRS} )
add_executable( ch1 curve_fitting.cpp )
# 与G2O和OpenCV链接
target_link_libraries( ch1
${OpenCV_LIBS}
g2o_core g2o_stuff
)
g2o 版本的库文件的不同直接决定了有些落后的CPP语法无法使用
此版本来源非最新版本,来自高翔GitHub上的库文件。
对应的版本文件
wpf@wpfpc:~/SLAM/SLAM14/slam-book-master_GaoXiang-master/3rdparty$ pwd
/home/wpf/SLAM/SLAM14/slam-book-master_GaoXiang-master/3rdparty
3. 常见问题及解决办法
在终端运行出错问题及解决办法:
wpf@wpfpc:~/SLAM/SLAM14/slam-book-master_GaoXiang-master/ch6/g2o_curve_fitting/build$ ./curve_fitting
./curve_fitting: symbol lookup error: ./curve_fitting: undefined symbol: _ZN3g2o30OptimizationAlgorithmLevenbergC1EPNS_6SolverE
wpf@wpfpc:~/SLAM/SLAM14/slam-book-master_GaoXiang-master/ch6/g2o_curve_fitting/build$ sudo ./curve_fitting
[sudo] password for wpf:
Sorry, try again.
[sudo] password for wpf:
generating data:
0 2.71828
0.01 2.93161
0.02 2.12942
0.03 2.46037
0.04 4.18814
0.05 2.73368
使用sudo ./curve_fitting 代替,./curve_fitting即可