Cmake编译PCL以及VS调试

使用Cmake编译程序
写在前头:本人是PCL小白,刚开始编译的时候,遇到很多问题,一开始在VS2017上直接配置PCL,遇到各种杂七杂八的bug,最后听从前人指导,说可以使用Cmake去编译程序,之前从未使用过Cmake,这次也只是浅浅的使用了一下,记录下来,一来是为了记录自己的成长,二来是分享给更多的人,共同交流学习~
我的配置是:64位win7旗舰版电脑、3.16.0的Cmake,1.9.1的PCL,以下是我的网盘链接(官网下载太慢了):
PCL链接:https://pan.baidu.com/s/1Q7Wq60HC-lxA2lFXBDOwpw
提取码:em7w
Cmake链接:https://pan.baidu.com/s/1ZL4B8VIpIU3JFQJUImiNZA
提取码:wv3z

1.新建三个文件,如下图所示:
Cmake编译PCL以及VS调试_第1张图片

2.三个文件的内容分别如下:
(1)CMakeList.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(Kd_tree)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (Kd_tree Kd_tree.cpp)
target_link_libraries (Kd_tree ${PCL_LIBRARIES})

(2)Kd_tree.cpp

#include 
#include 

#include 
#include 
#include 

int main(int argc, char** argv)
{
	srand(time(NULL)); //用系统时间初始化随机种子

	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);

	// Generate pointcloud data 随机点云生成
	cloud->width = 1000; //点云数量
	cloud->height = 1; //点云为无序点云
	cloud->points.resize(cloud->width * cloud->height);

	for (size_t i = 0; i < cloud->points.size(); ++i) //循环填充点云数据
	{
		cloud->points[i].x = 1024.0f * rand() / (RAND_MAX + 1.0f);
		cloud->points[i].y = 1024.0f * rand() / (RAND_MAX + 1.0f);
		cloud->points[i].z = 1024.0f * rand() / (RAND_MAX + 1.0f);
	}

	pcl::KdTreeFLANN<pcl::PointXYZ> kdtree; //创建kd-tree

	kdtree.setInputCloud(cloud); //设置搜索空间

	pcl::PointXYZ searchPoint;		//定义查询点并赋随机值
	searchPoint.x = 1024.0f * rand() / (RAND_MAX + 1.0f);
	searchPoint.y = 1024.0f * rand() / (RAND_MAX + 1.0f);
	searchPoint.z = 1024.0f * rand() / (RAND_MAX + 1.0f);

	// K nearest neighbor search k 近邻搜索
	int K = 10;
	std::vector<int> pointIdxNKNSearch(K);
	std::vector<float> pointNKNSquaredDistance(K);
	std::cout << "K nearest neighbor search at (" << searchPoint.x
		<< " " << searchPoint.y
		<< " " << searchPoint.z
		<< ") with K=" << K << std::endl;

	if (kdtree.nearestKSearch(searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0) //执行k近邻搜索
	{
		for (size_t i = 0; i < pointIdxNKNSearch.size(); ++i)
			std::cout << "    " << cloud->points[pointIdxNKNSearch[i]].x
			<< " " << cloud->points[pointIdxNKNSearch[i]].y
			<< " " << cloud->points[pointIdxNKNSearch[i]].z
			<< " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
	}

	// Neighbors within radius search 半径r内近邻搜索方式

	std::vector<int> pointIdxRadiusSearch; //存储近邻索引
	std::vector<float> pointRadiusSquaredDistance; //存储近邻对应的距离平方
	float radius = 256.0f * rand() / (RAND_MAX + 1.0f);

	std::cout << "Neighbors within radius search at (" << searchPoint.x
		<< " " << searchPoint.y
		<< " " << searchPoint.z
		<< ") with radius=" << radius << std::endl;


	if (kdtree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
	{
		for (size_t i = 0; i < pointIdxRadiusSearch.size(); ++i)
			std::cout << "    " << cloud->points[pointIdxRadiusSearch[i]].x
			<< " " << cloud->points[pointIdxRadiusSearch[i]].y
			<< " " << cloud->points[pointIdxRadiusSearch[i]].z
			<< " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
	}
	system("pause");
	return 0;
}

(3) cmake_bin是一个空的文件夹

3.在Cmake软件上编译:
Cmake编译PCL以及VS调试_第2张图片Cmake编译PCL以及VS调试_第3张图片
然后会看到原来的projec1文件夹中已经变模样了
Cmake编译PCL以及VS调试_第4张图片
4.在VS中打开工程文件
Cmake编译PCL以及VS调试_第5张图片

Cmake编译PCL以及VS调试_第6张图片
会发现有三个文件,先编译一次,如果没有错误,就可以Debug;如果出现打不开Debug里头的啥啥啥文件,那么就只留下Kd_tree即可,其余的两个文件移除掉。再次Debug即可。
效果如下:
Cmake编译PCL以及VS调试_第7张图片
成功了一次之后,将项目属性拷贝并记录下来,为下一次的配置做准备。

你可能感兴趣的:(3D)