当前位置:   article > 正文

CMake中CMakeLists文件的编写以及变量打印_cmakelist打印变量

cmakelist打印变量

最近在学习PCL,在Win10下使用VS编写PCL程序,配置环境时经常出错,踩坑记录详见:Win10+VS2017+PCL_1.8.1软件安装​​踩坑记录

看到《点云库PCL从入门到精通》书中使用的都是cmke,发现使用cmake更加简洁快速,不易出错,测试记录:Win10下使用Cmake编译PCL工程文件测试

CMakeLists文件的编写

使用cmake,最关键的一步就是编写CMakeLists文件。遂把PCL中常用的命令记录。

CMakeLists.txt文件示例:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

project(pcd_write)

find_package(PCL 1.2 REQUIRED)

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

add_executable (pcd_write pcd_write.cpp)
target_link_libraries (pcd_write ${PCL_LIBRARIES})

解释: 

定义最小版,这是强制性的cmake,因为正在做一个非常基本的项目,不需要从cmake 2.8或更高的功能。
cmake_minimum_required(version 2.8 FATAL_ERROR)

项目名称
project(pcd_write) //这里的pcd_write为工程文件夹名称

find_package()命令是用来查找依赖包的,可以把一整个依赖包的头文件包含路径、库路径、库名字、版本号等情况都获取到
find_package(PCL 1.2 REQUIRED)

我们要求找到最低版本为1.2的PCL软件包,如果找不到,将失败

由于PCL是模块化的,因此可以:

  • 只有一个组件: find_package(PCL 1.3 REQUIRED COMPONENTS io)
  • 一些: find_package(PCL 1.3 REQUIRED COMPONENTS io common)
  • 所有已存在: find_package(PCL 1.3 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

 当PCL被找到,一些相关的变量将会被设置:

  • PCL_FOUND:如果找到PCL,则设置为1,否则未设置

  • PCL_INCLUDE_DIRS:设置为PCL安装的标头和依赖标头的路径

  • PCL_LIBRARIES:设置为已建立和已安装的PCL库的文件名

  • PCL_LIBRARY_DIRS:设置为PCL库和第3方依赖关系所在的路径

  • PCL_VERSION:找到的PCL的版本

  • PCL_COMPONENTS:列出所有可用的组件

  • PCL_DEFINITIONS:列出所需的预处理程序定义和编译器标志

要让cmake知道在项目中包含的外部头文件,你需要使用include_directories宏。在我们的例子中,PCL_INCLUDE_DIRS包含了我们需要的内容,因此我们要求cmake搜索它包含的路径,以查找可能包含的头文件。

include_directories(${PCL_INCLUDE_DIRS})
参数形式 *_INCLUDE_DIRS,变量为 find_package路径和其他需要包含的路径
 

link_directories(${PCL_LIBRARY_DIRS})
用来加载外加的库函数路径,链接的库文件的路径,该指令有时候不一定需要。因为find_package和find_library指令可以得到库文件的绝对路径。

add_excutable(pcd_write pcd_write.cpp)
使用给定的源文件,为工程引入一个可执行文件

这里,我们告诉cmake我们试图从一个源文件pcd_write.cpp中创建一个名为pcd_write的可执行文件。CMake将添加后缀(Windows平台上的.exe和UNIX上的blank)和权限。

add_dependencies()
一般来说用不到。用到的情况就是两个targets有依赖关系(通过target_link_libraries解决)并且依赖库也是通过编译源码产生的。这时候一句add_dependencies可以在直接编译上层target时,自动检查下层依赖库是否已经生成。没有的话先编译下层依赖库,然后再编译上层target,最后link depend target

target_link_libraries(pcd_write ${PCL_LIBRARIES})
添加链接库;将目标文件与库文件进行链接.

我们正在构建的可执行文件调用PCL函数。到目前为止,我们只包含了PCL头文件,因此编译器知道我们正在调用的方法。我们还需要让链接器知道我们所链接的库。如前所述,使用PCL libraries变量引用PCL找到的库,剩下的就是触发我们调用目标链接库()宏所做的链接操作。

PCLConfig.cmake使用名为EXPORT的CMake特殊功能,该功能允许您使用其他项目目标,就像您自己构建它们一样。 使用此类目标时,它们称为导入目标,其作用与其他任何目标一样。

Cmake变量的打印

Cmake中打印消息方法为message,用于编译时期在cmake项目中输出日志信息,也可用于断点调试时,随时查看日志信息。 

message([<mode>] "message to display" ...)

message:消息名字,可在CMakeLists.txt或者.cmake脚本文件中输入,且有提示,不区分大小写
mode:打印消息的类别,有FATAL_ERROR,SEND_ERROR,WARNING,AUTHOR_WARNING,DEPRECATION,(none) or NOTICE,STATUS,VERBOSE,DEBUG,TRACE共10种
“message to display”:输出消息的内容,是字符串类型
…:表示可变参数,可连接多个输出

Mode- 打印消息的类别

  • FATAL_ERROR:cmake出错,停止编译和生成(信息红色)
  • SEND_ERROR:cmake出错,继续编译,但是停止生成(信息红色)
  • WARNING:cmake警告,继续编译(信息红色)
  • AUTHOR_WARNING:开发者警告,继续编译(信息红色)
  • DEPRECATION:如果使用set方法设置CMAKE_ERROR_DEPRECATED为true(不区分大小写),编译出错,否则继续编译
  • (none) or NOTICE:不设置mode,默认是NOTICE模式,不影响编译和生成,用于打印消息(信息白色)
  • STATUS:编译时状态信息,左边以--开头(信息白色)
  • DEBUG:针对开发人员的调试信息(信息白色)
  • TRACE:日志级别的临时信息(信息白色)
     

 使用示例:

  1. cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
  2. project(pcd_test)
  3. find_package(PCL 1.2 REQUIRED)
  4. include_directories(${PCL_INCLUDE_DIRS})
  5. link_directories(${PCL_LIBRARY_DIRS})
  6. add_definitions(${PCL_DEFINITIONS})
  7. SET(HELLO "Hello World")
  8. MESSAGE( STATUS "Hello = ${HELLO}.")
  9. MESSAGE( STATUS "PCL_FOUND = ${PCL_FOUND}.")
  10. MESSAGE( STATUS "PCL_INCLUDE_DIRS = ${PCL_INCLUDE_DIRS}.")
  11. MESSAGE( STATUS "PCL_LIBRARIES = ${PCL_LIBRARIES}.")
  12. MESSAGE( STATUS "PCL_LIBRARY_DIRS = ${PCL_LIBRARY_DIRS}.")
  13. MESSAGE( STATUS "PCL_VERSION = ${PCL_VERSION}.")
  14. MESSAGE( STATUS "PCL_COMPONENTS = ${PCL_COMPONENTS}.")
  15. MESSAGE( STATUS "PCL_DEFINITIONS = ${PCL_DEFINITIONS}.")
  16. add_executable (pcd_test pcd_test.cpp)
  17. target_link_libraries (pcd_test ${PCL_LIBRARIES})

输出结果:

  1. Hello = Hello World.
  2. PCL_FOUND = 1.
  3. PCL_INCLUDE_DIRS = D:/PCL191/include/pcl-1.9;D:/PCL191/3rdParty/Eigen/eigen3;D:/PCL191/3rdParty/Boost/include/boost-1_68;D:/PCL191/3rdParty/FLANN/include;D:/PCL191/3rdParty/VTK/include/vtk-8.1;D:/PCL191/3rdParty/OpenNI2/Include;D:/PCL191/3rdParty/Qhull/include.
  4. PCL_LIBRARIES = pcl_common;pcl_kdtree;pcl_octree;pcl_search;pcl_sample_consensus;pcl_filters;pcl_io;pcl_features;pcl_ml;pcl_segmentation;pcl_visualization;pcl_surface;pcl_registration;pcl_keypoints;pcl_tracking;pcl_recognition;pcl_stereo;pcl_outofcore;pcl_people;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_system-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_system-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_filesystem-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_filesystem-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_thread-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_thread-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_date_time-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_date_time-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_iostreams-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_iostreams-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_serialization-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_serialization-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_chrono-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_chrono-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_atomic-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_atomic-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Boost/lib/libboost_regex-vc141-mt-x64-1_68.lib;debug;D:/PCL191/3rdParty/Boost/lib/libboost_regex-vc141-mt-gd-x64-1_68.lib;optimized;D:/PCL191/3rdParty/Qhull/lib/qhullstatic.lib;debug;D:/PCL191/3rdParty/Qhull/lib/qhullstatic_d.lib;D:/PCL191/3rdParty/OpenNI2/Lib/OpenNI2.lib;optimized;D:/PCL191/3rdParty/FLANN/lib/flann_cpp_s.lib;debug;D:/PCL191/3rdParty/FLANN/lib/flann_cpp_s-gd.lib;vtksys;vtkCommonCore;vtkCommonMath;vtkCommonMisc;vtkCommonSystem;vtkCommonTransforms;vtkCommonDataModel;vtkCommonColor;vtkCommonExecutionModel;vtkCommonComputationalGeometry;vtkFiltersCore;vtkFiltersGeneral;vtkImagingCore;vtkImagingFourier;vtkalglib;vtkFiltersStatistics;vtkFiltersExtraction;vtkInfovisCore;vtkFiltersGeometry;vtkFiltersSources;vtkRenderingCore;vtkzlib;vtkfreetype;vtkRenderingFreeType;vtkRenderingContext2D;vtkChartsCore;vtkDICOMParser;vtklz4;vtkIOCore;vtkIOLegacy;vtkexpat;vtkIOXMLParser;vtkDomainsChemistry;vtkIOXML;vtkParallelCore;vtkFiltersAMR;vtkFiltersFlowPaths;vtkFiltersGeneric;vtkImagingSources;vtkFiltersHybrid;vtkFiltersHyperTree;vtkImagingGeneral;vtkFiltersImaging;vtkFiltersModeling;vtkFiltersParallel;vtkFiltersParallelImaging;vtkFiltersPoints;vtkFiltersProgrammable;vtkFiltersSMP;vtkFiltersSelection;vtkFiltersTexture;vtkFiltersTopology;verdict;vtkFiltersVerdict;vtkmetaio;vtkjpeg;vtkpng;vtktiff;vtkIOImage;vtkImagingHybrid;vtkInfovisLayout;vtkInteractionStyle;vtkImagingColor;vtkRenderingAnnotation;vtkRenderingVolume;vtkInteractionWidgets;vtkViewsCore;vtkproj4;vtkGeovisCore;vtkhdf5_hl;vtkhdf5;vtkIOAMR;vtkIOEnSight;vtkNetCDF;vtkexoIIc;vtkIOExodus;vtkRenderingOpenGL;vtkRenderingContextOpenGL;vtkgl2ps;vtkRenderingGL2PS;vtklibharu;vtkIOExport;vtkRenderingLabel;vtkIOExportOpenGL;vtkIOGeometry;vtkIOImport;vtklibxml2;vtkIOInfovis;vtkIOLSDyna;vtkIOMINC;vtkoggtheora;vtkIOMovie;vtknetcdfcpp;vtkIONetCDF;vtkIOPLY;vtkjsoncpp;vtkIOParallel;vtkIOParallelXML;vtksqlite;vtkIOSQL;vtkIOTecplotTable;vtkIOVideo;vtkImagingMath;vtkImagingMorphological;vtkImagingStatistics;vtkImagingStencil;vtkInteractionImage;vtkRenderingImage;vtkRenderingLIC;vtkRenderingLOD;vtkRenderingVolumeOpenGL;vtkViewsContext2D;vtkViewsInfovis.
  5. PCL_LIBRARY_DIRS = D:/PCL191/lib.
  6. PCL_VERSION = 1.9.1.
  7. PCL_COMPONENTS = pcl_common;pcl_kdtree;pcl_octree;pcl_search;pcl_sample_consensus;pcl_filters;pcl_io;pcl_features;pcl_ml;pcl_segmentation;pcl_visualization;pcl_surface;pcl_registration;pcl_keypoints;pcl_tracking;pcl_recognition;pcl_stereo;pcl_outofcore;pcl_people.
  8. PCL_DEFINITIONS = -D__SSSE3__;-D__SSE2__;-D__SSE__;-DBOOST_ALL_NO_LIB.

参考链接:

Using PCL in your own project — Point Cloud Library 0.0 documentation

https://blog.csdn.net/Calvin_zhou/article/details/104025714

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/394170
推荐阅读
相关标签
  

闽ICP备14008679号