当前位置: 首页 > 工具软件 > Go2o > 使用案例 >

CMakeLists.txt调用g2o优化库,解决找不到某些库文件问题

红甫
2023-12-01

解决生成的程序在运行时找不到某些链接库的错误: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

  1. 编译安装g2o.
    项目地址:https://github.com/RainerKuemmerle/g2o
    编译完成后,需要执行sudo make install安装到系统目录下。
  2. 在/etc/ld.so.conf中添加库的路径
    sudo gedit /etc/ld.so.conf
    
    加入以下路径:
    /usr/local/lib
    
    执行
    sudo ldconfig
    
  3. 编写CMakeLists.txt文件,通过FindG2O.cmake找到该库。
    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})
    
    编译运行即可。
 类似资料: