50 ubuntu下pcl编译以及用 VSCode配置pcl / opencv开发环境
0 引言
最近在VSCode下搞开发,于是将pcl库迁移到这个环境下,用来跑一些依赖pcl的开源的代码以及自己做一些快速开发等。
1 pcl编译
主要参考了这篇博客,链接如下。
https://blog.csdn.net/e_small/article/details/79581484
我编译时遇到的主要问题也是在这篇博客的留言下解决的。我安装了Anaconda,结果编译出错,我还一直找不着错哪儿了。。。解决方式记录如下。
$ sudo gedit ~/.bashrc # 打开环境变量文件 将Anaconda的环境变量给注销掉 $ source /etc/profile # 使环境变量生效 $ python # 测试目前系统默认的python是不是改正了
然后再重新编译。
另外,在编译时,我改变了CMakeList.txt中的配置,采用的方式是
$ mkdir build $ cd build $ cmake-gui .. # 打开cmake界面,把一些不需要编译的东西去掉(比如我为了提高编译成功率,去掉了cuda选项),客户端点击configuration-》 generation 即可完成cmake,再回到终端继续make $ make -j8 # 采用8个线程同时进行编译,有时候可以极大提高编译速度 $ sudo make install # 安装,该命令将pcl库的头文件、动态链接库文件、静态链接库文件和其他文件拷贝到/usr 的各个子目录下
2 opencv 编译
参考如下链接。
https://www.cnblogs.com/darkknightzh/p/5638117.html
3 VSCode下pcl配置文件编写
直接把自己的配置文件贴出来给大家看好了。
lauch.json
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}.out", "miDebuggerPath": "/usr/bin/gdb", "preLaunchTask":"build", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
tasks.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks":[ // 可以有多个参数 { "label": "build", // 编译任务名 "type": "shell", // 编译任务的类型,通常为shell/process类型 "command": "g++", // 编译命令 "args":[ "-g", "${workspaceFolder}/${fileBasename}", // include path指令 "-I", "/usr/local/include/pcl-1.8", "-I", "/usr/include/eigen3", "-I", "/usr/include/vtk-5.10", "-I", "/usr/include/qhull", "-I", "/usr/include/flann", "-I", "/usr/include/boost", // lib 库文件地址 "-L", "/usr/local/lib", "-l", "pcl_io", "-l", "pcl_visualization", "-l", "pcl_common", "-l", "vtkFiltering", "-l", "vtkCommon", "-l", "vtkRendering", "-l", "vtkGraphics", "-L", "/usr/include/x86_64-linux-gnu", "-l", "boost_system", "-o", // 生成指定名称的可执行文件 "${workspaceFolder}/${fileBasenameNoExtension}.out" ], "group": { "kind": "build", "isDefault": true } }, { "label": "cmakebuild", "type": "shell", "command": "cd build && cmake ../ && make", "args": [] } ] }
其中,采用cmake方式进行编译时的CMakeLists.txt文件是这样写的。
cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(myPCLProject) find_package(PCL 1.2 REQUIRED) include_directories(${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_definitions(${PCL_DEFINITIONS}) add_executable (cloud_viewer cloud_viewer.cpp) target_link_libraries (cloud_viewer ${PCL_LIBRARIES})
c_cpp_properties.json,主要是给intelliSense看的,避免写代码时,intelliSense瞎比划红线报错。
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "${workspaceFolder}", "/usr/local/include/pcl-1.8", "/usr/include", "/usr/include/vtk-5.10", "/usr/include/qhull", "/usr/include/flann", "/usr/include/boost", "/usr/include/eigen3", "/usr/include/eigen3/Eigen/", "/usr/include/x86_64-linux-gnu/sys" ], "defines": [], "browse":{ "path":[ "/usr/include", "/usr/local/include/pcl-1.8" ] }, "compilerPath": "/usr/bin/gcc", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 }
3 测试代码
是官网的一段可视化代码,展示如下。
#include <iostream> #include <pcl/io/io.h> #include <pcl/io/pcd_io.h> #include <pcl/visualization/cloud_viewer.h> int user_data; void viewerOneOff (pcl::visualization::PCLVisualizer& viewer) { viewer.setBackgroundColor (0.0, 0.0, 0.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 run once" << 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); //FIXME: possible race condition here: user_data++; } int main () { //pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>); pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile ("bun4.pcd", *cloud); pcl::visualization::CloudViewer viewer("Cloud 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; }
4 效果图