Ubuntu20.04 + VSCode + Cpp + PCL + OpenCV
准备工作
代码编辑:VSCode
开发语言:C++
编译工具:Cmake G++
依赖需求:PCL / OpenCV
安装PCL库
sudo apt install libpcl-dev
配置OpenCV库
- 安装依赖
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
-
下载源码
在两个网址下载source cide(zip)并解压缩
将opencv_contrib-4.5.3 放入opencv文件里并改名为opencv_contribhttps://github.com/opencv/opencv/releases/tag/4.5.3
https://github.com/opencv/opencv_contrib/releases/tag/4.5.3 -
编译源码
进入opencv文件夹
mkdir build
cd build/
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv4 -D OPENCV_EXTRA_MODULES_PATH=~/opencv-4.5.3/opencv_contrib/modules/ -D OPENCV_GENERATE_PKGCONFIG=YES -D OPENCV_ENABLE_NONFREE=True ..
解释如下
- CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ :
- 指定用g++编译, 需要自己用指令查找自己的g++ 安装位置,并进行替换
- CMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc :
- 指定用gcc编译, 需要自己用指令查找自己的gcc安装位置,并进行替换
- BUILD_opencv_cudacodec=OFF :
- 防止报与cuda相关的错误
- CMAKE_BUILD_TYPE=RELEASE :
- 表示编译发布版本
- CMAKE_INSTALL_PREFIX :
- 表示生成动态库的安装路径,可以自定义,考虑后续可能会安装opencv的不同版本,因此将路径变为CMAKE_INSTALL_PREFIX=/usr/local/opencv4
- OPENCV_GENERATE_PKGCONFIG=YES :
- 表示自动生成OpenCV的pkgconfig文件,否则需要自己手动生成
- OPENCV_EXTRA_MODULES_PATH=/home/autom/needs/opencv-4.5.3/opencv_contrib/modules/ -D :
- 不变为绝对路径cmake可能会报错,也可能 contrib 不能编译成功
问题1:卡在:IPPICV: Download: ippicv_2020_lnx_intel64_20191018_general.tgz
问题原因:这个包被墙了,网络不好下载不下来
解决办法:提前下载好再改路径解决
- 下载ippicv_2020_lnx_intel64_20191018_general.tgz
https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20191018/ippicv - 修改配置文件 ~/opencv-4.5.3/3rdparty/ippicv的ippicv.cmake 注意!不要用命令行打开,可能会出错 双击打开
- 找到该列
https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/ - 更改为ippicv_2020_lnx_intel64_20191018_general.tgz所在的文件夹路径,例如:/home/user/
参考网页:https://blog.csdn.net/qq_43478260/article/details/109458079
问题2:缺少boostdesc_bgm.i等文件
问题原因:未知
解决办法:下载好拷贝到目录下
- 下载文件 https://github.com/JZhong-Li/boostdesc_bgm.i
- 将所有文件拷贝到 opencv_contrib/modules/xfeatures2d/src/ 目录下
问题处理后重新cmake
然后查看CPU线程数
grep 'processor' /proc/cpuinfo | sort -u | wc -l
查到是48就用 -j48
sudo make -j48
sudo make install
配置VSCode
安装VSCode
官网选择ubuntu版本直接下载安装
https://code.visualstudio.com/Download
拓展插件:C/C++
/ C/C++ Extension Pack
/ CMake
/ CMake Tools
/ vscode-3d-preview
/ Include Autocomplete
新建代码结构
备注:共享文件夹内的代码不能直接链接调试,本地调试成功后再转移
文件目录如下
cpp
├── build
├── include
│ ├── func_common.h
│ ├── func_opencv.h
│ └── func_pcl.h
└── src
├── func_common.cpp
├── func_opencv.cpp
├── func_pcl.cpp
└── main.cpp
include下h包含自写的函数库,src里的main.cpp是源文件,其他cpp包含了对应函数库文件具体的实现
通过VSCode调试代码(增加Cmake文件)
20230416可用
新建cmake文件
文件目录如下
cpp
├── .vscode
│ ├── c_cpp_properties.json
│ ├── launch.json
│ └── tasks.json
├── build
│ └── bin
│ ├── Linx
│ └── data
├── CMakeLists.txt
├── data
├── include
│ ├── func_common.h
│ ├── func_opencv.h
│ └── func_pcl.h
└── src
├── func_common.cpp
├── func_opencv.cpp
├── func_pcl.cpp
└── main.cpp
需要的文件内容如下
1. CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(Linx)
# ========================================
# === 构建静态库以加速
# ========================================
set(COMMON_SOURCES src/func_common.cpp src/func_pcl.cpp src/func_opencv.cpp)
add_library(Func STATIC ${COMMON_SOURCES})
if(UNIX)
target_link_libraries(Func pthread)
endif()
# ========================================
# === PCL库
# ========================================
find_package(PCL 1.8 REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
# ========================================
# === OpenCV库
# ========================================
set(OpenCV_DIR "/home/acedia/libs/opencv-4.5.3/build/")
find_package(OpenCV REQUIRED)
if (NOT OpenCV_FOUND)
message(FATAL_ERROR "OpenCV library not found")
else()
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${OpenCV2_INCLUDE_DIRS})
link_directories(${OpenCV_LIB_DIR})
endif()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build/bin)
aux_source_directory(./src/ DIR_SRCS)
add_executable(Linx ${DIR_SRCS})
set_target_properties(Linx PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(Linx
${ABSOLUTE_TARGET_LIB} ${PCL_LIBRARIES} libpthread.so ${OpenCV_LIBS} Func ${CLOUD_VIEWER}
)
2. .vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include",
"/usr/include/**",
"/usr/include/x86_64-linux-gnu/sys",
// 检查pcl路径
"/usr/include/pcl-1.10",
"/usr/include/eigen3",
"/usr/include/eigen3/Eigen/",
"/usr/include/vtk-7.1",
"/usr/include/qhull",
"/usr/include/flann",
"/usr/include/boost",
// 检查opencv路径
"/usr/local/opencv4/include/opencv4"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
3. .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/bin/Linx",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
4. .vscode/tasks.json
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"type": "shell",
"label": "cmake",
"command": "cmake",
"args": [
".."
]
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "make",
"args": [
]
},
{
"label": "Build",
"dependsOrder": "sequence", // 按列出的顺序执行任务依赖项
"dependsOn":[
"cmake",
"make"
]
}
]
}
通过VSCode调试代码(修改.vscode
文件夹)
20230416更新:建议使用Cmake版本调试PCL和Opencv一起运行的代码
备注:文件的相对位置问题,如果运行main.out
文件则需要与其同目录,但是直接 F5
要放在main.cpp
目录
多文件调试设置,执行: ctrl
+ shift
+ p
: 选择配置任务
自动生成tasks.json文件
全选更改内容如下
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"${fileDirname}/func_common.cpp",
"${fileDirname}/func_pcl.cpp",
"${fileDirname}/func_opencv.cpp",
"-o",
"${workspaceFolder}/build/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
主要更改部分为选择的cpp以及生成可执行文件的位置
"-g",
"${file}",
"${fileDirname}/func_pcl.cpp",
"${fileDirname}/func_common.cpp",
"-o",
"${workspaceFolder}/build/${fileBasenameNoExtension}"
添加PCL和OpenCV等第三方库
执行: ctrl
+ shift
+ p
: 选择编辑配置
自动生成c_cpp_properties.json文件
在c_cpp_properties.json中加入pcl和opnecv库的路径
主要是给intelliSense看的,避免写代码时划红线报错
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
// 检查opencv路径
"/usr/local/opencv4/include/opencv4",
// 检查pcl路径
"/usr/include/pcl-1.10",
"/usr/include/eigen3",
"/usr/include/vtk-7.1"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
备注VSCode里文件变量含义
${workspaceRoot}
- 当前的文件夹的绝对路径+打开文件夹的名字
~/cpp/src
${workspaceFolder}
- 打开文件夹的绝对路径
~/cpp
${file}
- 当前打开正在编辑的文件名,包括绝对路径,文件名,文件后缀名
~/cpp/src/main.cpp
${relativeFile}
- 从当前打开的文件夹到当前打开的文件的路径
src/main.cpp
${fileBasename}
- 当前打开的文件名+后缀名,不包括路径
main.cpp
${fileBasenameNoExtension}
- 当前打开的文件的文件名,不包括路径和后缀名
main
${fileDirname}
- 当前打开的文件所在的绝对路径,不包括文件名
~/cpp/src
${fileExtname}
- 当前打开的文件的文件名,不包括路径和后缀名
main
${cwd}
- 打开的文件夹
cpp
在tasks.json中加入第三方库的路径
注意:与依赖Cmake不同,这种方式需要链接其他库,秘诀是通过报错增加链接.so文件
执行时的依赖(20230328可用)
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks":[ // 可以有多个参数
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args":[
"-g",
"${file}",
"${fileDirname}/func_common.cpp",
"${fileDirname}/func_pcl.cpp",
"${fileDirname}/func_opencv.cpp",
// include path指令
"-I", "/usr/local/opencv4/include/opencv4",
"-I", "/usr/local/opencv4/include/opencv4/opencv2",
"-I", "/usr/local/include",
"-I", "/usr/include/pcl-1.10",
"-I", "/usr/include/eigen3",
"-I", "/usr/include/vtk-7.1",
// "-I", "/usr/include/qhull",
// "-I", "/usr/include/flann",
// "-I", "/usr/include/boost",
// lib 库文件地址
"-L", "/usr/lib/x86_64-linux-gnu",
// 基本库
"-l", "pcl_io",
"-l", "pcl_common",
// 点云可视化
"-l", "pcl_visualization",
"-l", "vtkCommonCore-7.1",
"-l", "vtkCommonMath-7.1",
"-l", "vtkRenderingCore-7.1",
"-l", "vtkCommonDataModel-7.1",
// 点云滤波
"-l", "pcl_filters",
// 点云欧式聚类
"-l", "pcl_search",
"-l", "pcl_segmentation",
// 点云区域生长
"-l", "pcl_features",
// "-l", "pcl_kdtree",
// "-l", "pcl_surface",
// "-l", "pcl_tracking",
// "-l", "boost_system",
"-o",
// 生成指定名称的可执行文件
"${workspaceFolder}/build/${fileBasenameNoExtension}.out"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "cmakebuild",
"type": "shell",
"command": "cd build && cmake ../ && make",
"args": []
}
]
}
调试离群点移除时报错如下
error: ‘flann_algorithm_t’ was not declared in this scope
取消调用OpenCV库后正常使用,验证是OpenCV库和flann冲突导致
解决办法:20230413更新,如果只使用PCL的flann库,可以通过修改OpenCV库中的flann文件夹名避免:
执行如下两步,进入路径,更改名称,解决!
cd /usr/local/opencv4/include/opencv4/opencv2
sudo mv flann/ flann2/
20230413更新,治标不治本,需要使用opencv的flann库时也不能正常调用,除非把所有涉及opencv/flann换成opencv/flann2,需要重新思考解决方案
考虑通过cmake版调试解决该问题