本文主要分享我在Ubuntu下安装PCL点云库的经验,这里我用的ubuntu是22.04版本,依赖库基本都下载的新的,VTK版本是8.2(建议不要使用更新版本),PCL版本是1.12.1。
这里建议安装虚拟机,可以参照ubuntu20.04安装教程进行安装,需要注意的是安装时候要分配足够的磁盘空间,因为如果后面扩展磁盘空间的话会比较麻烦,扩展硬盘空间可以参照扩展磁盘进行操作,建议不要走到这一步。
这里参考VMware虚拟机不能全屏的解决办法进行操作,也就是将设置中的分辨率改一下。
有了ubuntu之后首先要做的就是换上国内的镜像源,这里可以参照Ubuntu换源的两种方法进行操作,这样能使我们的下载速度得到明显提升。
这里参照Ubuntu18安装VTK8.2进行操作,但是make时候出现了问题。
[ 87%] Linking C shared library ../../../lib/libvtkexodusII-8.2.so
/usr/bin/ld: CMakeFiles/vtkexodusII.dir/src/ex_open_par.c.o:/home/littlez/下载/Software/VTK-8.2.0/ThirdParty/exodusII/vtkexodusII/src/ex_open_par.c:477: multiple definition of `exodus_unused_symbol_dummy_1'; CMakeFiles/vtkexodusII.dir/src/ex_create_par.c.o:/home/littlez/下载/Software/VTK-8.2.0/ThirdParty/exodusII/vtkexodusII/src/ex_create_par.c:617: first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [ThirdParty/exodusII/vtkexodusII/CMakeFiles/vtkexodusII.dir/build.make:4436:lib/libvtkexodusII-8.2.so.1] 错误 1
make[1]: *** [CMakeFiles/Makefile2:5836:ThirdParty/exodusII/vtkexodusII/CMakeFiles/vtkexodusII.dir/all] 错误 2
make: *** [Makefile:136:all] 错误 2
可以看到这是multiple definition of
‘exodus_unused_symbol_dummy_1’,处理办法也是简单粗暴,找到其中一个定义,然后注释掉。
[ 99%] Building CXX object Rendering/Qt/CMakeFiles/vtkRenderingQt.dir/vtkQtLabelRenderStrategy.cxx.o
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtLabelRenderStrategy.cxx: In member function ‘virtual void vtkQtLabelRenderStrategy::ComputeLabelBounds(vtkTextProperty*, vtkUnicodeString, double*)’:
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtLabelRenderStrategy.cxx:229:18: error: aggregate ‘QPainterPath path’ has incomplete type and cannot be defined
229 | QPainterPath path;
| ^~~~
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtLabelRenderStrategy.cxx: In member function ‘virtual void vtkQtLabelRenderStrategy::RenderLabel(int*, vtkTextProperty*, vtkUnicodeString, int)’:
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtLabelRenderStrategy.cxx:331:16: error: aggregate ‘QPainterPath path’ has incomplete type and cannot be defined
331 | QPainterPath path;
| ^~~~
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtLabelRenderStrategy.cxx: In member function ‘virtual void vtkQtLabelRenderStrategy::RenderLabel(int*, vtkTextProperty*, vtkUnicodeString)’:
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtLabelRenderStrategy.cxx:442:18: error: aggregate ‘QPainterPath path’ has incomplete type and cannot be defined
442 | QPainterPath path;
| ^~~~
make[2]: *** [Rendering/Qt/CMakeFiles/vtkRenderingQt.dir/build.make:104:Rendering/Qt/CMakeFiles/vtkRenderingQt.dir/vtkQtLabelRenderStrategy.cxx.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:7066:Rendering/Qt/CMakeFiles/vtkRenderingQt.dir/all] 错误 2
make: *** [Makefile:136:all] 错误 2
以及
[ 99%] Building CXX object Rendering/Qt/CMakeFiles/vtkRenderingQt.dir/vtkQtStringToImage.cxx.o
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtStringToImage.cxx: In member function ‘virtual vtkVector2i vtkQtStringToImage::GetBounds(vtkTextProperty*, const vtkUnicodeString&, int)’:
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtStringToImage.cxx:109:16: error: aggregate ‘QPainterPath path’ has incomplete type and cannot be defined
109 | QPainterPath path;
| ^~~~
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtStringToImage.cxx: In member function ‘virtual vtkVector2i vtkQtStringToImage::GetBounds(vtkTextProperty*, const vtkStdString&, int)’:
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtStringToImage.cxx:140:16: error: aggregate ‘QPainterPath path’ has incomplete type and cannot be defined
140 | QPainterPath path;
| ^~~~
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtStringToImage.cxx: In member function ‘virtual int vtkQtStringToImage::RenderString(vtkTextProperty*, const vtkUnicodeString&, int, vtkImageData*, int*)’:
/home/littlez/下载/Software/VTK-8.2.0/Rendering/Qt/vtkQtStringToImage.cxx:187:16: error: aggregate ‘QPainterPath path’ has incomplete type and cannot be defined
187 | QPainterPath path;
| ^~~~
make[2]: *** [Rendering/Qt/CMakeFiles/vtkRenderingQt.dir/build.make:118:Rendering/Qt/CMakeFiles/vtkRenderingQt.dir/vtkQtStringToImage.cxx.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:7066:Rendering/Qt/CMakeFiles/vtkRenderingQt.dir/all] 错误 2
make: *** [Makefile:136:all] 错误 2
这里就是aggregate ‘QPainterPath path’ has incomplete type and cannot be defined
处理办法也是简单地在对应文件中加入
#includ <QPainterPath>
这样算是能完成编译,后续如有问题还会更新。
接下来要做的就是安装PCL了,这里可以参照ubuntu18.04 安装pcl(详细教程)进行操作,目前还没遇到问题,能完成下载,后续使用一段时间后再来分享。这里有条命令分享一下:
sudo apt-cache search libxxx #查看libxxx可供下载的版本
在编译VTK和PCL时我们一般会用多线程同时编译以节省时间,也就是
make -j2 #这里的2就代表编译所用的线程数
然而这也会带来很大的内存开销,当内存不足时,make就会闪退,以我的实际经历来说,给了虚拟机8G内存,开两个线程make,结果还是会内存爆掉,只开一个线程才能正常make过去。
配置C++环境可以参照vscode环境中配置C++环境,配置PCL则可以参照vscode配置OpenCV+PCL进行操作,但是由于PCL为1.12.1版本,VTK为8.2版本,这里在配置时c_cpp_properties.json、launch.json和tasks.json都需要一定的修改,这里我是按如下进行设置。
注意:这里的配置还不完善,仅供参考。
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}",
"/usr/local/include/pcl-1.12",
"/usr/include",
"/usr/include/vtk-7.1",
"/usr/include/libqhull",
"/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.12"
]
},
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x64"
// "cStandard": "gnu17",
// "cppStandard": "gnu++17",
// "intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
// "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"program": "${workspaceFolder}/main.out",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "build",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
{
// 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",
"command":"g++",
"args": [
"-g",
"${workspaceFolder}/${fileBasename}",
"-I","/usr/local/include/pcl-1.12",
"-I","/usr/include/eigen3",
"-I","/usr/include/vtk-7.1",
"-I","/usr/include/libqhull",
"-I","/usr/include/flann",
"-I","/usr/include/boost",
"-L","/usr/local/lib",
"-l","pcl_io",
"-l","pcl_visualization",
"-l","pcl_common",
// "-l","vtkFiltering",
// "-l","vtkCommon",
// "-l","vtkRendering",
// "-l","vtkGraphics",
"-l","vtkFiltersCore-8.2",
"-l","vtkCommonCore-8.2",
"-l","vtkRenderingCore-8.2",
"-l","vtkGeovisCore-8.2",
"-I","/usr/include/x86_64-linux-gnu",
"-l","boost_system",
"-o",
"${workspaceFolder}/main.out"
],
"group":{
"kind": "build",
"isDefault": true
}
}
// ,
// {
// "label": "build",
// "type": "shell",
// "command": "g++",
// "args": ["-g","${file}","-o","${fileBasenameNoExtension}.out"]
// }
]
}
这里尤其需要注意的是tasks.json文件,本质上就是把命令行要输入的命令写在这个文件里了,有兴趣的朋友可以参考一下Linux下G++编译。
用以上文件内容替换其他配置教程中对应文件中的内容,再按照教程里操作步骤进行操作即可将代码编译通过,生成可执行程序。
相比配置VScode来编译代码,用cmake来编译可能才是更多人的选择。可以参考PCL配置,这里也简单介绍一下。
就是先在工程的文件夹中新建一个CMakeLists.txt,内容根据自己的需要来填,如:
cmake_minimum_required(VERSION 3.0)
project(testpcl)
find_package(PCL 1.2 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable(testpcl testpcl.cpp)
target_link_libraries (testpcl ${PCL_LIBRARIES})
install(TARGETS testpcl RUNTIME DESTINATION bin)
然后在终端中依次输入:
mkdir build
cd build
cmake ..
make
./testpcl #注意这里要根据实际填写
这样能完成编译并执行生成的可执行程序,这里在执行可执行文件的时候还遇到了一个问题。
littlez@littlez-virtual-machine:~/Learn/build$ ./testpcl
./testpcl: error while loading shared libraries: libvtkfreetype-8.2.so.1: cannot open shared object file: No such file or directory
这里参照解决可执行文件运行时报错及export LD_LIBRARY_PATH 的使用里的内容,在 ~ 路径下的 .bashrc 文件中加入下面语句
export LD_LIBRARY_PATH=LD_LIBRARY_PATH:/usr/local/lib
为解决主机与虚拟机之间文件传输问题,这里我们使用xshell和xftp来实现,具体流程可以参照xshell连接ubuntu。
泪目了,家人们!
这里要说的就是,make时候心中默念“佛祖保佑”,这样可以给cmake加buff(不是)。遇到困难不要怕,微笑地面对她!如果真的遇到问题,要相信我所走过的坑前人一定都走过,细心一点,看是报了什么错,然后去搜答案抄作业。如果真遇到了前人没踩过的坑,那就勇于做第一个踩坑人吧~~~
以上内容希望能给各位提供一定参考,最后祝各位都能successful !!!