CMake入門之建立一個基於PCL的最小工程

     最近在學習PCL,藉助Cmake可省去繁瑣的添加包含目錄和依賴庫操做。ide

     一個典型的CMakeLists.txt內容一般爲:函數

1 cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
2 project(MY_GRAND_PROJECT)
3 find_package(PCL 1.3 REQUIRED COMPONENTS common io)
4 include_directories(${PCL_INCLUDE_DIRS})
5 link_directories(${PCL_LIBRARY_DIRS})
6 add_definitions(${PCL_DEFINITIONS})
7 add_executable(pcd_write_test pcd_write.cpp)
8 target_link_libraries(pcd_write_test ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES})

CMake文件第一行:學習

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

這一句對Cmake來講是必需的,須要在這句添加知足你對Cmake特徵需求的最小版本號。ui

接下來一句:spa

project(MY_GRAND_PROJECT)

創建一個工程,括號內MY_GRAND_PROJECT爲本身工程的名字。code

find_package(PCL 1.3 REQUIRED COMPONENTS common io)

因爲咱們是創建一個PCL項目,所以須要找到對應的PCL package,若是找不到則項目建立失敗。除此以外,咱們還能夠使用一下方式:component

1)若是是須要某一個PCL的某一個組件: find_package(PCL 1.3 REQUIRED COMPONENTS io)blog

2)若是是幾個組件:find_package(PCL 1.3 REQUIRED COMPONENTS io common)ci

3)若是須要整個安裝包:find_package(PCL 1.3 REQUIRED)get

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

當PCL安裝包找到以後,就須要添加對應的包含目錄和依賴庫了。咱們須要設置幾個相關的變量:

  • PCL_FOUND: set to 1 if PCL is found, otherwise unset
  • PCL_INCLUDE_DIRS: set to the paths to PCL installed headers and the dependency headers
  • PCL_LIBRARIES: set to the file names of the built and installed PCL libraries
  • PCL_LIBRARY_DIRS: set to the paths to where PCL libraries and 3rd party dependencies reside
  • PCL_VERSION: the version of the found PCL
  • PCL_COMPONENTS: lists all available components
  • PCL_DEFINITIONS: lists the needed preprocessor definitions and compiler flags
add_executable(pcd_write_test pcd_write.cpp)

接下來這須要從pcd_write.cpp文件生成一個名爲pcd_write_test的可執行文件。

在生成對應的exe文件以後,須要調用PCL相關函數,所以須要添加相應連接庫:

target_link_libraries(pcd_write_test ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES})

至此,就能夠使用CMake生成本身的工程了。

相關文章
相關標籤/搜索