最近在學習PCL,借助Cmake可省去繁瑣的添加包含目錄和依賴庫操作。
一個典型的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特征需求的最小版本號。
接下來一句:
project(MY_GRAND_PROJECT)
建立一個工程,括號內MY_GRAND_PROJECT為自己工程的名字。
find_package(PCL 1.3 REQUIRED COMPONENTS common io)
由於我們是建立一個PCL項目,因此需要找到對應的PCL package,如果找不到則項目創建失敗。除此之外,我們還可以使用一下方式:
1)如果是需要某一個PCL的某一個組件: find_package(PCL 1.3 REQUIRED COMPONENTS io)
2)如果是幾個組件:find_package(PCL 1.3 REQUIRED COMPONENTS io common)
3)如果需要整個安裝包:find_package(PCL 1.3 REQUIRED)
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生成自己的工程了。