g2o的安装和配置

单独使用 g2o.cmake

把所有配置放到文件g2o.cmake,然后在CMakeLists里添加

1
2
3
4
5
6
7
8
set(ALL_TARGET_LIBRARIES "")
include(g2o.cmake)

# 最后添加
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
${ALL_TARGET_LIBRARIES}
)

无需再添加eigen的头文件目录

安装和配置

我目前用的还是g2o旧版本,如果程序对应的是新版本,可能出现这样的报错

1
error: ‘make_unique’ is not a member of ‘g2o’

g2o的CMakeLists.txt的OpenMP部分

1
2
3
4
5
6
7
8
9
10
11
12
# Eigen parallelise itself, OPENMP is experimental. We experienced some slowdown with it
set(G2O_USE_OPENMP OFF CACHE BOOL "Build g2o with OpenMP support (EXPERIMENTAL)")
if(G2O_USE_OPENMP)
find_package(OpenMP)
if(OPENMP_FOUND)
MESSAGE(" ----------------- Using OpenMP ! ----------------- ")
set (G2O_OPENMP 1)
set(g2o_C_FLAGS "${g2o_C_FLAGS} ${OpenMP_C_FLAGS}")
set(g2o_CXX_FLAGS "${g2o_CXX_FLAGS} -DEIGEN_DONT_PARALLELIZE ${OpenMP_CXX_FLAGS}")
message(STATUS "Compiling with OpenMP support")
endif(OPENMP_FOUND)
endif(G2O_USE_OPENMP)

使用OpenMP编译安装g2o,即set(G2O_USE_OPENMP ON),但是随即编译我的程序报错:
1
2
3
/usr/bin/ld: CMakeFiles/ls_slam_g2o.dir/src/main.cpp.o: undefined reference to symbol 'omp_set_lock@@OMP_3.0'
//usr/lib/x86_64-linux-gnu/libgomp.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

搜索发现函数void lock() { omp_set_lock(&_lock); } 定义在 openmp_mutex.h

CMakeLists.txt中添加:

1
2
3
4
5
6
7
8
FIND_PACKAGE(OpenMP)

target_link_libraries(node
${catkin_LIBRARIES}
${CSPARSE_LIBRARY}
${G2O_LIBS}
OpenMP::OpenMP_CXX
)

代码的CMake配置

CMakeLists.txt如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
cmake_minimum_required(VERSION 2.8.3)
project(node)

add_compile_options(-std=c++11)
set(CMAKE_BUILD_TYPE "Realease")

LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)

# find g2o lib
find_package(G2O REQUIRED)
IF(G2O_FOUND)
include_directories(${G2O_INCLUDE_DIR})
message(" -------- G2O lib is found-------- : "${G2O_INCLUDE_DIR})
ENDIF(G2O_FOUND)

find_package(Eigen3 REQUIRED)
find_package(CSparse REQUIRED)

find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
# g2o的库文件也可能在 /usr/local/lib
LINK_DIRECTORIES(/opt/ros/melodic/lib)

SET(G2O_LIBS g2o_cli g2o_ext_freeglut_minimal g2o_simulator g2o_solver_slam2d_linear g2o_types_icp g2o_types_slam2d g2o_core g2o_interface g2o_solver_csparse g2o_solver_structure_only g2o_types_sba g2o_types_slam3d g2o_csparse_extension g2o_opengl_helper g2o_solver_dense g2o_stuff g2o_types_sclam2d g2o_parser g2o_solver_pcg g2o_types_data g2o_types_sim3 cxsparse )

catkin_package(
# INCLUDE_DIRS include
# LIBRARIES ls_slam
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)

include_directories(
include
${catkin_INCLUDE_DIRS}
${EIGEN_INCLUDE_DIRS}
${CSPARSE_INCLUDE_DIR}
)

add_executable(node src/main.cpp)

target_link_libraries(node ${catkin_LIBRARIES} ${CSPARSE_LIBRARY} ${G2O_LIBS})

g2o 找不到Eigen

编译g2o结果报错fatal error: Eigen/Core: No such file or directory compilation terminated.

FindEigen3.cmake 使用以下代码段:

1
2
3
4
5
6
find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library
PATHS
${CMAKE_INSTALL_PREFIX}/include
${KDE4_INCLUDE_DIR}
PATH_SUFFIXES eigen3 eigen
)

所以它默认CMAKE_INSTALL_PREFIX在Linux主机上查找/usr/local

打开可视化工具g2o_viewer和一个示例,如果能使用,就正常了
g2o_viewer
manhattan3500 dataset

参考:
《SLAM十四讲》中g2o的安装
示例和配置
编译错误