CMake入门之创建一个基于PCL的最小工程

     最近在学习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生成自己的工程了。

原文地址:https://www.cnblogs.com/freshmen/p/5828867.html