CMakeLists.txt calls the g2o optimization library to solve the problem that some library files cannot be found

Solve the error that some link libraries cannot be found when the generated program is running: error while loading shared libraries: libg2o_stuff.so: cannot open shared object file: No such file or directory: 1. Install g2o to the system directory, 2. Add the library path /usr/local/ in /etc/ld.so.conf lib

  1. Compile and install g2o.
    Project address: https://github.com/RainerKuemmerle/g2o
    After compiling, it needs to be sudo make installinstalled in the system directory.
  2. Add the path of the library in /etc/ld.so.conf
    sudo gedit /etc/ld.so.conf
    
    Add the following paths:
    /usr/local/lib
    
    implement
    sudo ldconfig
    
  3. Write a CMakeLists.txt file and find the library through 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})
    
    Just compile and run.

Guess you like

Origin blog.csdn.net/weixin_43196818/article/details/125635124