How To Write CmakeLists.Txt
Cmakelists.txt是作为Cmake building system的输入文件,用于构建软件包。 一、CmakeList.txt的简单例子
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
#set(project_namecloud_viewer_PointXYZ) project(project_name)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS})
add_executable (project_namecloud_viewer_PointXYZ.cpp) #注意这里不能替换 target_link_libraries (project_name ${PCL_LIBRARIES}) 二、解析
CmakeLists.txt的重要组成部分: 1、表明Cmake所需要的最低版本
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
2、定义需要的特殊变量(optional)(关于set的用法我目前没有细看,有心的同学自己钻研)
SET(sampleNameMyApp)
如上面的例子:set(project_namecloud_viewer_PointXYZ). find_package(PCL 1.2 REQUIRED) #主要依赖的package
#REQUIRED表示如果没有找到,cmake会停止处理,并报告一个错误.如果找到了package那么将会生成几个包含package信息的Cmake环境变量,这些变量将会在后面的cmake script中用到。这些环境变量描述了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}) #包含头文件的位置 link_directories(${PCL_LIBRARY_DIRS}) #添加链接器的lib库文件路径 add_definitions(${PCL_DEFINITIONS})
除此之外也可以手动添加特殊路径如: include_directories(\
include_directories(\3、查找我们构建工程所需要的package
link_directories(\link_directories(\ 5、从指定源文件构建可执行文件
add_executable (project_namecloud_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.hgrab.hinterface.h test.cpp test.h)
6、为project构建library
add_library(${project_name} ${${project_name}_src}) #默认创建共享library
7、指定可执行文件需要连接的库
target_link_libraries (project_name${PCL_LIBRARIES})#前一个参数为可执行文件的名字
target_link_libraries (project_name ${PCL_LIBRARIES} libeng.lib libmx.lib libmex.lib libmat.lib Aria.lib
winmm.lib wsock32.lib) TARGET_LINK_LIBRARIES( ${project_name}
${MRPT_LIBS} # This is filled by FIND_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
但是在运行时很可能出现错误:
fatal error LNK1104: 无法打开文件“C:\\Qt\\4.8.0\\lib\\QtGuid4.lib”,这主要是因为VTK库里面依赖函数的关系不对应,我的QT版本是Qt5.2.1所以必须修改这些依赖函数。
解决方法是打开路径C:\\Program Files\\PCL 1.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
intuser_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, \std::cout<< \ } void
viewerPsycho (pcl::visualization::PCLVisualizer& viewer)
{
static unsigned count = 0; std::stringstreamss;
ss<< \viewer.removeShape (\
viewer.addText (ss.str(), 200, 300, \可¨|以°?注á?é释o¨a掉ì?这a行D,ê?此??时o?à不?会¨¢再¨′显?示o?图a?形?中D显?示o?Once per viewer loop:num一°?直?à增?加¨?
//FIXME: possible race condition here: user_data++; } int main () {
pcl::visualization::CloudViewer viewer(\
//blocks until the cloud is actually rendered viewer.showCloud(cloud);
//use the following functions to get access to the underlying more advanced/powerful //PCLVisualizer
//This will only get called once
viewer.runOnVisualizationThreadOnce (viewerOneOff);
//This will get called once per visualization iteration viewer.runOnVisualizationThread (viewerPsycho); while (!viewer.wasStopped ()) {
//you can also do cool processing here
//FIXME: Note that this is running in a separate thread from viewerPsycho //and you should guard against race conditions yourself... user_data++; } return 0; }
二、激光扫描仪;
立体摄像机和飞行摄像机获得的点云数据是有序点云数据,类似于图像或矩阵的结构,数据分为行和列;它相比于无序点云的优点在于预先了解相邻点的关系,邻域操作更加高效。
pcl::io::loadPCDFile (\
pcl::PointCloud