make skills

1. install到指定路径,避免破坏本机环境

示例

  1. 安装Ceres-2.1.0,cmake后添加install路径

    1. cd ~/Downloads
      wget https://github.com/ceres-solver/ceres-solver/archive/refs/tags/2.1.0.tar.gz -O ceres-solver-2.1.0.tar.gz
      tar -xvf ceres-solver-2.1.0.tar.gz -c ~/catkin_ws/src
      cd ~/catkin_ws/src/ceres-solver-2.1.0
      mkdir build && cd build
      cmake .. -DCMAKE_INSTALL_PREFIX=~/catkin_ws/install/ceres-solver_2.1.0 -DCMAKE_PREFIX_PATH=~/catkin_ws/install/glog_0.4.0
      make -j8
      make install
      
      1
      2
      3
      4
      5
      6

      2. 安装Outram

      1. ```JSON
      git clone [email protected]:Pamphlett/Outram.git
      cd Outram && mkdir build && cd build
    2. 修改CMakeLists.txt

    3. # 指定 glog 和 ceres 的路径
      set(GLOG_DIR "~/catkin_ws/install/glog_0.4.0")
      set(CERES_DIR "~/catkin_ws/install/ceres-solver_2.1.0/lib/cmake/Ceres")
      
      # 添加外部库
      find_package(Ceres REQUIRED PATHS ${CERES_DIR})
      include_directories(${GLOG_DIR}/include)
      link_directories(${GLOG_DIR}/lib)
      
      target_link_libraries(global_localization
              PUBLIC
              ${PCL_LIBRARIES}
              ${catkin_LIBRARIES}
              ${OpenMP_LIBS}
              ${CERES_LIBRARIES}
              glog
              stdc++fs
              pmc
      )
      
      1
      2
      3
      4
      5
      6
      7
      8

      4. 或者cli camke时手动添加

      ```Plain
      cmake .. -DGLOG_DIR=~/catkin_ws/install/glog_0.4.0 -DCERES_DIR=~/catkin_ws/install/ceres-solver_2.1.0/lib/cmake/Ceres
      mv pmc-src/ ../../../build/
      cd ~/catkin_ws
      catkin build outram

2. **CMAKE_INSTALL_PREFIX **

步骤分析:

  1. 定义:指定安装目录的根路径
  2. 默认值:通常是

local

  1. 作用:影响 make install 的安装位置
  2. 常见用例:避免系统权限问题或自定义安装路径

使用示例:

1
2
3
4
5
# 设置安装路径到用户目录下
cmake -DCMAKE_INSTALL_PREFIX=/home/user/myapp ..

# 设置安装路径到自定义位置
cmake -DCMAKE_INSTALL_PREFIX=/opt/myapp ..

对应的目录结构将变为:

1
2
3
4
5
${CMAKE_INSTALL_PREFIX}/
├── bin/ # 可执行文件
├── lib/ # 库文件
├── include/ # 头文件
└── share/ # 文档和资源

在 CMakeLists.txt 中的使用:

1
2
3
4
5
install(TARGETS myapp
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)

3. CMAKE_PREFIX_PATH

CMAKE_PREFIX_PATH 是 CMake 中的一个重要变量,用于指定查找依赖库和包的搜索路径。

步骤说明:

  1. 用于查找第三方库的位置
  2. 可以设置多个搜索路径
  3. 帮助 CMake 的 find_package() 命令定位依赖

用法示例:

1
2
3
4
5
# 单个路径
cmake -DCMAKE_PREFIX_PATH=/opt/qt5 ..

# 多个路径 (用分号分隔)
cmake -DCMAKE_PREFIX_PATH=/opt/qt5;/usr/local/boost ..

典型场景:

  • Qt库的查找
  • Boost库的查找
  • 其他第三方库的查找

在 CMakeLists.txt 中的相关使用:

1
2
find_package(Qt5 REQUIRED)
find_package(Boost REQUIRED)

如果没有设置 CMAKE_PREFIX_PATH,CMake 会在以下默认路径查找:

  • /usr/local/
  • /usr/
  • /opt/