有时候需要直接运行小型的单个程序,一般指仅包含一个main函数的程序,这时候在ubantu系统下直接运行大概有以下几种情况:
1.对于没有使用依赖包的程序,如仅是进行运算,输出等,没有使用外部依赖包如pcl,eigen等,客直接在程序所在位置打开终端,运行:
g++ test1.cpp -o test1
test1为cpp文件名
另外还可以利用vscode插件,code runner,直接点击右上角三角即可运行
2,对于有需要依赖包的程序,就需要编写相应的cmake文件。例如:有下方一个点云配准的程序
程序来自博客
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/registration/icp.h>
#include <pcl/visualization/pcl_visualizer.h>//可视化头文件
#include <pcl/filters/filter.h>
#include <pcl/filters/radius_outlier_removal.h>
#include <pcl/filters/conditional_removal.h>
#include <pcl/registration/gicp.h>
#include <pcl/common/time.h>
#include <pcl/filters/voxel_grid.h>
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
int main(int argc, char** argv)
{
// 创建点云指针
PointCloudT::Ptr cloud_in(new PointCloudT); // 需要转换的点云
PointCloudT::Ptr cloud_tr(new PointCloudT); // 转换后的点云
PointCloudT::Ptr cloud_icped(new PointCloudT); // ICP后输出点云
PointCloudT::Ptr cloud_icped1(new PointCloudT); // ICP后输出点云
PointCloudT::Ptr cloud_aim(new PointCloudT); // 向其转换的目标点云
PointCloudT::Ptr cloud_in_src(new PointCloudT);
PointCloudT::Ptr cloud_aim_src(new PointCloudT);
PointCloudT::Ptr cloud_in_rad(new PointCloudT);
PointCloudT::Ptr cloud_aim_rad(new PointCloudT);
pcl::StopWatch timeer;
//读取pcd文件
if (pcl::io::loadPCDFile<pcl::PointXYZ>("test_pcd_seg_0.pcd", *cloud_aim_src) == -1)
{
PCL_ERROR("Couldn't read file1 \n");
return (-1);
}
std::cout << "Loaded " << cloud_aim_src->size() << " data points from file1" << std::endl;
if (pcl::io::loadPCDFile<pcl::PointXYZ>("test_pcd_seg_1.pcd", *cloud_in_src) == -1)
{
PCL_ERROR("Couldn't read file2 \n");
return (-1);
}
std::cout << "Loaded " << cloud_in_src->size() << " data points from file2" << std::endl;
std::vector<int> mapping_in;
std::vector<int> mapping_out;
pcl::removeNaNFromPointCloud(*cloud_in_src, *cloud_in_src, mapping_in);
pcl::removeNaNFromPointCloud(*cloud_aim_src, *cloud_aim_src, mapping_out);
// Perform the actual filtering
pcl::RadiusOutlierRemoval<PointT> outrem;
// build the filter
outrem.setInputCloud(cloud_in_src);
outrem.setRadiusSearch(0.04);
outrem.setMinNeighborsInRadius(80);
// apply filter
outrem.filter(*cloud_in_rad);
// build the filter
outrem.setInputCloud(cloud_aim_src);
outrem.setRadiusSearch(0.04);
outrem.setMinNeighborsInRadius(80);
// apply filter
outrem.filter(*cloud_aim_rad);
// downsample clouds
pcl::VoxelGrid<PointT> vg;
vg.setInputCloud(cloud_in_rad);
vg.setLeafSize(0.01f, 0.01f, 0.01f);
vg.filter(*cloud_in);
vg.setInputCloud(cloud_aim_rad);
vg.setLeafSize(0.01f, 0.01f, 0.01f);
vg.filter(*cloud_aim);
timeer.reset();
//icp配准
//pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp; //创建ICP对象,用于ICP配准
//pcl::GeneralizedIterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp;
//icp.setMaximumIterations(500); //设置最大迭代次数iterations=true
//icp.setMaxCorrespondenceDistance(0.5);
//icp.setTransformationEpsilon(1e-6);
//icp.setEuclideanFitnessEpsilon(1);
//icp.setInputCloud(cloud_in); //设置输入点云
//icp.setInputTarget(cloud_aim); //设置目标点云(输入点云进行仿射变换,得到目标点云)
//icp.align(*cloud_icped1); //匹配后源点云
pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp2; //创建ICP对象,用于ICP配准
//icp2.setEuclideanFitnessEpsilon(1);
icp2.setMaximumIterations(200);
icp2.setInputCloud(cloud_in); //设置输入点云
icp2.setInputTarget(cloud_aim); //设置目标点云(输入点云进行仿射变换,得到目标点云)
icp2.align(*cloud_icped); //匹配后源点云
//icp.setMaximumIterations(1); // 设置为1以便下次调用
//std::cout << "Applied " << iterations << " ICP iteration(s)" << std::endl;
if (icp2.hasConverged())//icp.hasConverged ()=1(true)输出变换矩阵的适合性评估
{
std::cout << "\nICP has converged, score is: " << icp2.getFitnessScore() << std::endl;
std::cout << "\nICP has converged, Epsilon is: " << icp2.getEuclideanFitnessEpsilon() << std::endl;
//std::cout << "\nICP transformation " << iterations << " : cloud_in -> cloud_aim" << std::endl;
std::cout << "\nICP transformation is \n " << icp2.getFinalTransformation() << std::endl;
//transformation_matrix = icp.getFinalTransformation().cast<double>();
//print4x4Matrix(transformation_matrix);
}
else
{
PCL_ERROR("\nICP has not converged.\n");
return (-1);
}
std::cout << "ICP run time: " << timeer.getTimeSeconds() << " s" << std::endl;
//可视化
pcl::visualization::PCLVisualizer viewer("ICP demo");
// 创建两个观察视点
int v1(0);
int v2(1);
viewer.createViewPort(0.0, 0.0, 0.5, 1.0, v1);
viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);
// 定义显示的颜色信息
float bckgr_gray_level = 0.0; // Black
float txt_gray_lvl = 1.0 - bckgr_gray_level;
// 原始的点云设置为白色的
pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_aim_color_h(cloud_aim, (int)255 * txt_gray_lvl, (int)255 * txt_gray_lvl,
(int)255 * txt_gray_lvl);
viewer.addPointCloud(cloud_aim, cloud_aim_color_h, "cloud_aim_v1", v1);//设置原始的点云都是显示为白色
viewer.addPointCloud(cloud_aim, cloud_aim_color_h, "cloud_aim_v2", v2);
// 需要被转换的点云显示为绿色
pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_in_color_h(cloud_in, 20, 180, 20);
viewer.addPointCloud(cloud_in, cloud_in_color_h, "cloud_in_v1", v1);
// ICP配准后的点云为红色
pcl::visualization::PointCloudColorHandlerCustom<PointT> cloud_icped_color_h(cloud_icped, 180, 20, 20);
viewer.addPointCloud(cloud_icped, cloud_icped_color_h, "cloud_icped_v2", v2);
// 加入文本的描述在各自的视口界面
//在指定视口viewport=v1添加字符串“white 。。。”其中"icp_info_1"是添加字符串的ID标志,(10,15)为坐标16为字符大小 后面分别是RGB值
viewer.addText("White: Original point cloud\nGreen: Matrix transformed point cloud", 10, 15, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "icp_info_1", v1);
viewer.addText("White: Original point cloud\nRed: ICP aligned point cloud", 10, 15, 16, txt_gray_lvl, txt_gray_lvl, txt_gray_lvl, "icp_info_2", v2);
// 设置背景颜色
viewer.setBackgroundColor(bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v1);
viewer.setBackgroundColor(bckgr_gray_level, bckgr_gray_level, bckgr_gray_level, v2);
// 设置相机的坐标和方向
viewer.setCameraPosition(-3.68332, 2.94092, 5.71266, 0.289847, 0.921947, -0.256907, 0);
viewer.setSize(1280, 1024); // 可视化窗口的大小
//显示
while (!viewer.wasStopped())
{
viewer.spinOnce();
}
return 0;
}
这时候要运行就需要使用到PCL库,所以需要编写CmakeLists.txt文件,不知道怎么写的可以找其他人用到PCL的代码的CmakeLists.txt文件,复制过来自己改写,大体上都差不多。
对应上述程序的CmakeLists.txt文件如下:
cmake_minimum_required ( VERSION 2.6 FATAL_ERROR) #对于cmake版本的最低版本的要求
project(transfer_pcdfile) #建立的工程名,例如源代码目录路径的变量名为CH_DIR
find_package(PCL 1.7 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS} )
link_directories(${PCL_LIBRARIES_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(test1 test1.cpp)
target_link_libraries(test1 ${PCL_COMMON_LIBRARIES} ${PCL_IO_LIBRARIES} ${PCL_FILTERS_LIBRARIES} ${PCL_VISUALIZATION_LIBRARIES} ${PCL_REGISTRATION_LIBRARIES} )
注意:若在make时候报如下错误
test1.cpp:(.text._ZN3pcl13visualization13PCLVisualizer13addPointCloudINS_8PointXYZEEEbRKNS_10PointCloudIT_E8ConstPtrERKNS0_22PointCloudColorHandlerIS5_EERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi[_ZN3pcl13visualization13PCLVisualizer13addPointCloudINS_8PointXYZEEEbRKNS_10PointCloudIT_E8ConstPtrERKNS0_22PointCloudColorHandlerIS5_EERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi]+0xd2):对‘pcl::visualization::PointCloudGeometryHandlerXYZ<pcl::PointXYZ>::PointCloudGeometryHandlerXYZ(boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ> const> const&)’未定义的引用
CMakeFiles/test1.dir/test1.cpp.o:在函数‘bool pcl::visualization::PCLVisualizer::fromHandlersToScreen<pcl::PointXYZ>(pcl::visualization::PointCloudGeometryHandler<pcl::PointXYZ> const&, pcl::visualization::PointCloudColorHandler<pcl::PointXYZ> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&, Eigen::Quaternion<float, 0> const&)’中:
test1.cpp:(.text._ZN3pcl13visualization13PCLVisualizer20fromHandlersToScreenINS_8PointXYZEEEbRKNS0_25PointCloudGeometryHandlerIT_EERKNS0_22PointCloudColorHandlerIS5_EERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiRKN5Eigen6MatrixIfLi4ELi1ELi0ELi4ELi1EEERKNSL_10QuaternionIfLi0EEE[_ZN3pcl13visualization13PCLVisualizer20fromHandlersToScreenINS_8PointXYZEEEbRKNS0_25PointCloudGeometryHandlerIT_EERKNS0_22PointCloudColorHandlerIS5_EERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiRKN5Eigen6MatrixIfLi4ELi1ELi0ELi4ELi1EEERKNSL_10QuaternionIfLi0EEE]+0x27d):对‘pcl::visualization::PCLVisualizer::createActorFromVTKDataSet(vtkSmartPointer<vtkDataSet> const&, vtkSmartPointer<vtkLODActor>&, bool)’未定义的引用
在target_link_libraries一行中将头文件中包含的库都给链接上,换成大写就行,仿照着写。
改写完成之后,一样的新建build,cmake …,build
就可以直接运行了。