原文见:https://blog.csdn.net/bactone_li/article/details/26454123
Cmakelists.txt是作为Cmake building system的输入文件,用于构建软件包。

一、CmakeList.txt的简单例子

cmake_minimum_required(VERSION2.8 FATAL_ERROR)

#set(project_namecloud_viewer_PointXYZ)

project(project_name)

find_package(PCL1.2 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})

_directories(${PCL_LIBRARY_DIRS})

add_definitions(${PCL_DEFINITIONS})

add_executable (project_name cloud_viewer_PointXYZ.cpp) #注意这里不能替换

target_ _libraries(project_name ${PCL_LIBRARIES})

二、解析

CmakeLists.txt的重要组成部分:

1、表明Cmake所需要的最低版本

cmake_minimum_required(VERSION2.8 FATAL_ERROR)

2、定义需要的特殊变量(optional)(关于set的用法我目前没有细看,有心的同学自己钻研)

SET(sampleName MyApp)

     如上面的例子:set(project_name cloud_viewer_PointXYZ).

3、查找我们构建工程所需要的package

     find_package(PCL 1.2 REQUIRED) #主要依赖的package

#REQUIRED表示如果没有找到,cmake会停止处理,并报告一个错误. 如果找到了package那么将会生成几个包含package信息的Cmake环境变量,这些变量将会在后面的cmake 中用到。这些环境变量描述了package的外部头文件位置(include路径),依赖的库文件的位置(lib),以及源程序的位置。例如上面命令执行找到PCL后,将会创建环境变量PCL_INCLUDE_DIRS其中包含指定PCL库头文件.h的查找路径;创建环境变量PCL_LIBRARY_DIRS,其中包含指定PCL库的.lib文件的所在目录的路径.

如果所要构建的project还依赖于其它的package,例如python那么可以采用命令:

find_package(PCL 1.2 REQUIRED COMPONENTS python),

此时python将作为PCLpackage的组成成分,它所对应的头文件include路径和lib库文件路径都将被一起包含在PCL的对应环境变量PCL_INCLUDE_DIRS和PCL_LIBRARY_DIRS中,方便后面使用。

4、指定构建project所需要的资源

include_directories(${PCL_INCLUDE_DIRS})#包含头文件的位置

_directories(${PCL_LIBRARY_DIRS})#添加链接器的lib库文件路径

add_definitions(${PCL_DEFINITIONS})

除此之外也可以手动添加特殊路径如:
include_directories(“G:/Matlab/extern/include”)
include_directories(“C:/Program Files/MobileRobots/Aria/include”)

_directories(“G:/Matlab/extern/lib/win32/microsoft”)
_directories(“C:/Program Files/MobileRobots/Aria/lib”)

5、从指定源文件构建可执行文件

add_executable (project_name cloud_viewer_PointXYZ.cpp)

该命令将从源文件cloud_viewer_PointXYZ.cpp构建可执行程序project_name.exe.

如果从多个源文件构建可执行程序则可以表示成:

add_executable (project_namemain.cpp test1.cpp test2.cpp)

add_executable (project_namemain.cpp part.h grab.h interface.h test.cpp test.h)

6、为project构建library

add_library(${project_name} KaTeX parse error: Expected \'}\', got \'EOF\' at end of input: {{project_name}_src})#默认创建共享library

7、指定可执行文件需要连接的库

     target_ _libraries (project_name ${PCL_LIBRARIES})#前一个参数为可执行文件的名字

     target_ _libraries (project_name ${PCL_LIBRARIES} libeng.lib libmx.lib libmex.lib libmat.lib Aria.lib winmm.libwsock32.lib)

TARGET_ _LIBRARIES(

${project_name}
${MRPT_LIBS} # This is filled byFIND_PACKAGE(MRPT …)
“” # Optional extra libs…
)

三、利用Cmake来构建程序

基本的CmakeLists.txt已经写好,那么接下来就是:

1、 新建一个文件夹source,如:F:\\study\\program_software\\VC++program\\PCL\\PCL_study\\cloud_viewer_PointXYZ\\source

将CmakeLists.txt,所有的自己编写的源文件(.c or .cpp)以及头文件(.h)放在该文件夹下。

2、 再在与source同目录的文件加下建一个新的文件夹cmake-bin,如:

F:\\study\\program_software\\VC++program\\PCL\\PCL_study\\cloud_viewer_PointXYZ\\cmake-bin

该文件将用来放置构建的project。

3、 打开Cmake软件界面及设置如下:

4、先点击配置configure,此时可能会报错如Could not copy from:C:/Program Files/CMake2.8.12.2/share/cmake-2.8/Templates/CMakeVSMacros2.vsmacros to: d:/用户目录/Documents/VisualStudio 010/Projects/VSMacros80/CMakeMacros/CMakeVSMacros2.vsmacros
的红色字样警告,因为这是系统用户的路径名有中文字符“用户目录”造成的,因为cmake不认中文路径,但是只要我们在上面的第三步中设置生成的路径中没有中文就行了。就算有这个因为C:/Users/下有中文照成的错误,也对我们这次的生成无碍,不用去管他。

再点击一次configure时会快速完成配置。最后出现configuring done。

5、点击generate

当看到generating done时,说明生成成功了。

此时在cmake-bin文件夹下生成如下内容:

6、打开Proj_Name.sln

将Proj_Name设置成启动项

7、开始debug

但是在运行时很可能出现错误:

fatalerror LNK1104: 无法打开文件“C:\\Qt\\4.8.0\\lib\\QtGuid4.lib”,这主要是因为VTK库里面依赖函数的关系不对应,我的QT版本是Qt5.2.1所以必须修改这些依赖函数。

解决方法是打开路径C:\\Program Files\\PCL1.6.0\\3rdParty\\VTK\\lib\\vtk-5.8下的三个文件:

VTKConfigQt.cmake

VTKTargets-debug.cmake

VTKTargets-release.cmake

将其中的路径C:\\Qt\\4.8.0替换成你现有版本的Qt路径,例如我的是:C:\\Qt\\Qt5.2.1\\5.2.1\\msvc2010_opengl,(当然也可以将这个路径定义为环境变量 $(QT_ROOT));然后将对应的库文件如QtGuid4.lib替换成你自己安装的Qt版本的库文件如Qt5Guid.lib,最终即可正常运行。

8、正常运行,结果如下:

9、源程序和3D点云:

cloud_viewer_PointXYZ.cpp如下:

#include <pcl/visualization/cloud_viewer.h>

#include

#include <pcl/io/io.h>

#include <pcl/io/pcd_io.h>

int user_data;

void

viewerOneOff(pcl::visualization::PCLVisualizer& viewer)

{

viewer.setBackgroundColor (1.0, 0.5, 1.0);

pcl::PointXYZ o;

o.x = 1.0;

o.y = 0;

o.z = 0;

viewer.addSphere (o, 0.25, \"sphere\",0);

std::cout << \"i only runonce\" << std::endl;

}

void

viewerPsycho(pcl::visualization::PCLVisualizer& viewer)

{

static unsigned count = 0;

std::stringstream ss;

ss << \"Once per viewer loop:\" << count++;

viewer.removeShape (\"text\", 0);

viewer.addText (ss.str(), 200, 300, \"text\",0);//可¨¦以°?注Á¡é释º¨ª掉Ì?这a行D,ê?此ä?时º¡À不?会¨¢再¨´显?示º?图ª?形?中D显?示º?Once per viewer loop:num一°?直¡À增?加¨®



//FIXME: possible race condition here:

user_data++;

}

int

main ()

{

     pcl::PointCloud<pcl::PointXYZ>::Ptrcloud (new pcl::PointCloud<pcl::PointXYZ>); //modified



     pcl::io::loadPCDFile (\"point_cloud_XYZ.pcd\",*cloud);



pcl::visualization::CloudViewer viewer(\"CloudViewer\");



//blocks until the cloud is actuallyrendered

viewer.showCloud(cloud);



//use the following functions to get accessto the underlying more advanced/powerful

//PCLVisualizer



//This will only get called once

viewer.runOnVisualizationThreadOnce(viewerOneOff);



//This will get called once pervisualization iteration

viewer.runOnVisualizationThread(viewerPsycho);

while (!viewer.wasStopped ())

{

//you can also do cool processing here

//FIXME: Note that this is running in aseparate thread from viewerPsycho

//and you should guard against race conditionsyourself...

user_data++;

}

return 0;

作者:bactone_li
来源:CSDN

收藏 打印