解决生成的程序在运行时找不到某些链接库的错误:error while loading shared libraries: libg2o_stuff.so: cannot open shared object file: No such file or directory
: 1. 将g2o安装到系统目录下,2. 并且在/etc/ld.so.conf中添加库的路径 /usr/local/lib
sudo make install
安装到系统目录下。sudo gedit /etc/ld.so.conf
加入以下路径:/usr/local/lib
执行sudo ldconfig
cmake_minimum_required(VERSION 3.14)
project(g2oDemo)
IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
MESSAGE("Build type: " ${CMAKE_BUILD_TYPE})
list(APPEND CMAKE_MODULE_PATH g2o路径/cmake_modules)
# C++14 support
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Eigen3 3.3 REQUIRED)
find_package(G2O REQUIRED)
include_directories(${G2O_INCLUDE_DIR})
message(STATUS "G2O_INCLUDE_DIR: ${G2O_INCLUDE_DIR}")
message(STATUS "G2O_FOUND: ${G2O_FOUND}")
SET(G2O_LIBRARIES ${G2O_STUFF_LIBRARY} ${G2O_CORE_LIBRARY} ${G2O_CLI_LIBRARY} ${G2O_SOLVER_CHOLMOD} ${G2O_SOLVER_CSPARSE} ${G2O_SOLVER_CSPARSE_EXTENSION}
${G2O_SOLVER_DENSE} ${G2O_SOLVER_PCG} ${G2O_SOLVER_SLAM2D_LINEAR} ${G2O_SOLVER_STRUCTURE_ONLY} ${G2O_SOLVER_EIGEN} ${G2O_TYPES_DATA} ${G2O_TYPES_ICP} ${G2O_TYPES_SBA}
${G2O_TYPES_SCLAM2D} ${G2O_TYPES_SIM3} ${G2O_TYPES_SLAM2D} ${G2O_TYPES_SLAM3D})
add_executable(ba_demo ba_demo.cpp)
target_link_libraries(ba_demo ${G2O_LIBRARIES})
编译运行即可。