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生成本身的工程了。

相关文章
相关标签/搜索