在ubuntu上安装pcl库并配置vscode使用cmake生成可执行文件

文章目录

  • 在ubuntu上安装pcl库并配置vscode使用cmake生成可执行文件
    • 先列举一下我踩的坑:
    • 一、安装vtk7.1
      • QT5.12.6
      • VTK7.1.1
    • 二、安装pcl1.9.1
      • 安装依赖库
      • 安装PCL库
    • 三、使用vscode+cmake编译例程

在ubuntu上安装pcl库并配置vscode使用cmake生成可执行文件

在ubuntu上安装配置pcl的时候踩了不少坑,最后使用vscode+cmake搞定了整套流程。

先列举一下我踩的坑:

  • pcl1.9.1需要调用vtk7.1的库文件,但是vtk并没有完全被安装,单独安装vtk7.1可以解决问题;
  • 安装vtk7.1的时候需要提前并安装下载Qt5,否则不能成功Configure;
  • vscode中手动设置task.json需要为g++添加许多include和库文件,这些文件很难找全导致编译失败,使用cmake并编写配套的CMakeList.txt可以解决这个问题;
  • vscode中包含的库文件出现红色下划线,需要在c_cpp_properties.json中设置compileCommands文件的路径,并在tasks.json中设置cmake生成compile_commands.json文件;
  • 文件已经成功编译了,但是显示出几个奇怪的方框,让我不知道其实我已经编译成功了,只需要按’r’就调整视角即可。

完整的正确流程如下:

一、安装vtk7.1

这里参考这篇博文:https://blog.csdn.net/weixin_46474546/article/details/104711901

  • QT(http://download.qt.io/archive/qt/)
  • VTK7.1.1(https://vtk.org/download/)
  • PCL1.9.1(https://github.com/PointCloudLibrary/pcl/releases)

QT5.12.6

  • 下载(https://download.qt.io/archive/qt/5.12/5.12.6/qt-opensource-linux-x64-5.12.6.run)

  • 下载文件夹下运行

    chmod +x qt-opensource-linux-x64-5.12.6.run
    ./qt-opensource-linux-x64-5.12.6.run
    
  • 安装的时候记住要安装全部的Qt文件,只勾选开发工具是不可以的

VTK7.1.1

  • 下载tar.gz包(https://www.vtk.org/files/release/7.1/VTK-7.1.1.tar.gz)

  • 安装X11,openGL,CMake

sudo apt-get install libx11-dev libxext-dev libxtst-dev libxrender-dev libxmu-dev libxmuu-dev
sudo apt-get install build-essential libgl1-mesa-dev libglu1-mesa-dev
sudo apt-get install cmake cmake-qt-gui
  • CMake生成工程
  1. 解压VTK-7.1.1.tar.gz,在VTK-7.1.1文件夹中建立build文件夹
  2. 打开CMake软件GUI界面,设置“where is the source code:” 为源代码所在目录/VTK-7.1.1。设置“where to build the binaries:”为工程生成的目录/VTK-7.1.1/build(手动新建一个)
  3. 点击“Configure”按钮,在弹出对话框中选择“Unix MakeFiles”
  4. 选中“VTK_GROUP_Qt”选项,再次点击“Configure”按钮,提示警告信息,需要设置Qt5安装路径
  5. VTK_QT_VERSION 选择 “5” 再次点击“Configure”按钮
  6. Qt5_DIR设置路径为QT的安装目录 /opt/Qt5.12.6/5.12.6/gcc_64/lib/cmake/Qt5
  7. 提示“Configure done”,点击“Generate”按钮,在build中生成工程文件
  • 编译安装

在build目录中运行下面的命令

sudo make
sudo make install

二、安装pcl1.9.1

安装依赖库

  • PCL依赖Boost,Eigen,FlANN,VTK,OpenNI,QHull,metslib等第三方库:
sudo apt-get install libflann1.8 libflann-dev
sudo apt-get install libeigen3-dev
sudo apt-get install libboost-all-dev 
sudo apt-get install cmake cmake-qt-gui libusb-1.0-0-dev libusb-dev libudev-dev  mpi-default-dev openmpi-bin openmpi-common  libqhull* libgtest-dev freeglut3-dev pkg-config  libxmu-dev libxi-dev mono-complete libopenni-dev libopenni2-dev libpng-dev 
sudo apt-get install libpcap-dev
sudo apt-get install libglew-dbg libglew-dev libglew1.13 libglewmx-dbg libglewmx-dev libglewmx1.13 glew-utils
sudo apt-get install libqhull*
  • 安装metslib库:
wget http://www.coin-or.org/download/source/metslib/metslib-0.5.3.tgz
tar zxvf metslib-0.5.3.tgz
cd metslib-0.5.3
./configure
make
sudo apt-get install autoconf automake libtool
sudo make install

安装PCL库

  • 下载pcl文件(https://github.com/PointCloudLibrary/pcl/archive/refs/tags/pcl-1.9.1.tar.gz),如果有网络问题建议在网上找同名包

  • 解压并进入解压出的PCL-1.9.1文件夹,运行

mkdir build && cd build
cmake  -D CMAKE_BUILD_TYPE=None  -D BUILD_GPU=ON  -D BUILD_apps=ON  -D BUILD_examples=ON ..
sudo make
sudo make install

三、使用vscode+cmake编译例程

参考:http://vegetable.wodekouwei.com/2019/04/21/l-cmake-syntax/

  • 默认已经完成了vscode以及插件的安装

  • 建立如下的文件结构,其中.vscode和build是文件夹

.
├── .vscode
├──├──c_cpp_properties.json
├──├──launch.json
├──├──tasks.json 
├── build
├── CMakeLists.txt
└── pcl_test.cpp

  • c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include",
                "/usr/local/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}
  • 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/pcl_test",// 表示可执行程序所在的路径,其中,${workspaceRoot}表示VScode加载的文件夹的根目录
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "make build"
        }
    ]
}
  • tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make build",//编译的项目名,build
            "type": "shell",
            "command": "cd ./build ;cmake ../ -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ;make",//编译命令
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "make clean",
            

        }
    ]
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(pcl_test)

find_package(PCL 1.9 REQUIRED)

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable(pcl_test pcl_test.cpp)

target_link_libraries (pcl_test ${PCL_LIBRARIES})

install(TARGETS pcl_test RUNTIME DESTINATION bin)
  • pcl_test.cpp
#include 
#include 
#include 
#include 

int user_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, "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::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
    pcl::io::loadPCDFile ("cat.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;
}
  • F5运行,生成的可执行文件就在build文件夹下,名字为pcl_test

你可能感兴趣的:(pcl,ubuntu,c++)