今天第一次使用VScode在Linux上成功运行调试了一个C++程序,特此记录相关代码
首先是 RegTest.cpp:
这里的一些PCL的头文件,可以通过VS code的小灯泡智能补全到c_cpp_properties.json文件中。
#include <iostream> //标准输入输出头文件
#include <pcl/io/pcd_io.h> //I/O操作头文件
#include <pcl/point_types.h> //点类型定义头文件
#include <pcl/registration/icp.h> //ICP配准类相关头文件
int main (int argc, char** argv)
{
//创建两个pcl::PointCloud<pcl::PointXYZ>共享指针,并初始化它们
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_out (new pcl::PointCloud<pcl::PointXYZ>);
// 随机填充点云
cloud_in->width = 5; //设置点云宽度
cloud_in->height = 1; //设置点云为无序点
cloud_in->is_dense = false;
cloud_in->points.resize (cloud_in->width * cloud_in->height);
for (size_t i = 0; i < cloud_in->points.size (); ++i)
{
cloud_in->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud_in->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud_in->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
}
std::cout << "Saved " << cloud_in->points.size () << " data points to input:"//打印处点云总数
<< std::endl;
for (size_t i = 0; i < cloud_in->points.size (); ++i) std::cout << " " << //打印处实际坐标
cloud_in->points[i].x << " " << cloud_in->points[i].y << " " <<
cloud_in->points[i].z << std::endl;
*cloud_out = *cloud_in;
std::cout << "size:" << cloud_out->points.size() << std::endl;
//实现一个简单的点云刚体变换,以构造目标点云
for (size_t i = 0; i < cloud_in->points.size (); ++i)
cloud_out->points[i].x = cloud_in->points[i].x + 0.7f;
std::cout << "Transformed " << cloud_in->points.size () << " data points:"
<< std::endl;
for (size_t i = 0; i < cloud_out->points.size (); ++i) //打印构造出来的目标点云
std::cout << " " << cloud_out->points[i].x << " " <<
cloud_out->points[i].y << " " << cloud_out->points[i].z << std::endl;
pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp; //创建IterativeClosestPoint的对象
icp.setInputCloud(cloud_in); //cloud_in设置为点云的源点
icp.setInputTarget(cloud_out); //cloud_out设置为与cloud_in对应的匹配目标
pcl::PointCloud<pcl::PointXYZ> Final; //存储经过配准变换点云后的点云
icp.align(Final); //打印配准相关输入信息
std::cout << "has converged:" << icp.hasConverged() << " score: " <<
icp.getFitnessScore() << std::endl;
std::cout << icp.getFinalTransformation() << std::endl;
return (0);
}
然后是CMakeLists.txt:
# 使用CMake Tools插件(可选,如果这个项目去到一个没有这个插件的机器也同样可以生成项目)
include(CMakeToolsHelpers OPTIONAL)
cmake_minimum_required(VERSION 2.8.3)
project(RegTest)
find_package(PCL 1.7 REQUIRED)
# 指定构建project所需要的资源
include_directories(${PCL_INCLUDE_DIRS})#包含头文件的位置
link_directories(${PCL_LIBRARY_DIRS})#添加链接器的lib库文件路径
add_definitions(${PCL_DEFINITIONS})
#除此之外也可以手动添加特殊路径如:
#include_directories("G:/Matlab/extern/include")
#include_directories("C:/Program Files/MobileRobots/Aria/include")
add_executable (RegTest src/RegTest.cpp)
# add_executable (RegTest src/plyReg.cpp)
#在生成对应的exe文件之后,需要调用PCL相关函数,因此需要添加相应链接库:
target_link_libraries(RegTest ${PCL_LIBRARIES})
CMakeLists.txt的写法还需要学习很多。
现在在CMake Tool的帮助下,可以自动的建立一个build文件夹,最后生成的可执行文件就会在 ./build/文件夹下面。
最后是launch.json:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/RegTest",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
// "preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
在这个json文件中第一要注意修改“program”后面的可执行程序的路径和名字,这个是用CMake Tool直接build出来的。另外,这里就不需要"preLaunchTask"了,因为已经用CMake build好了。