任务描述:ubuntu下在vs code内配置opencv的c++环境, 并运行示例代码。
step 0. 写在前面的话
我的环境是Ubuntu18,OpenCV3.4.1+contrib3.4.1,VScode都是已经安装好的。
其中opencv的配置请参考ubuntu下opencv的配置。VScode是直接在商城中搜索下载即可。
step 1. vs code插件安装
如下图,给vs code安装C/C++插件。
step 2. 新建一个文件夹,在vs code内新建文件,编写如下调试代码。
我们的配置过程分为两个阶段:C++编译环境和opencv开发环境。
step 3. C++编译环境的配置
Debug -> Open Configurations -> 打开备选框 -> C++(GDB/LLDB) -> g++ build and debug active file
上述操作打开launch.json文件,修改其中的设置项。修改后如下:
"version": "0.2.0",
"configurations": [
{
"name": "g++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
其中需要重点注意的几个地方在于:
(1)"externalConsole": true 关键字修改为true,指的是弹出命令窗口。
(2)"preLaunchTask": "g++ build active file" 后面的配置需要用到这个关键字。
然后回到测试代码的文件,注销掉与opencv相关的代码(因为)我们目前仅针对C++编译环境进行配置,点击F5运行,出现如下提示:
选择Configure Task,在备选框中选择C/C++:cpp build active file选项,则新建一个task.json文件。
修改文件配置如下:
//> 编译单个文件
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "g++",
"args": [
"-g","-std=c++11", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
//> 编译多个文件
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "g++",
"args": [
"-g","-std=c++11", "${fileDirname}/*.cpp", "-o", "${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
其中,需要注意的几点是:
(1)"label": "g++ build active file" 关键字必须与前述launch.json文件的"preLaunchTask": "g++ build active file"关键字一致。
(2)"command": "g++" 关键字要修改为g++,否则可能会报不能debug的提示。
(3)"args": [ "-g", "-std=c++11", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ] 关键字就是g++的编译指令。
此时,就可以运行CPP代码了,回到测试文件,点击F5,会在控制台输出Hello world的字符,如图。
step 4. 配置Opencv的开发环境
这一步的主要思路,与在windows环境下为VS配置Opencv开发环境是一致的,即:将include的头文件所在路径和函数库的路径
设置给编译器。
Ctrl + Shift + P 打开搜索框,键入c++,会出现备选项目,选择图示Edit configurations (JSON),
进入c_cpp_properties.json文件内进行配置修改。修改如下:
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
主要注意的地方在于:
"includePath" 选项内添加关键字路径 "/usr/local/include",这里面存放的是opencv和opencv2两个文件,如图:
另外在task.json文件内添加如下修改:
"args": [
"-g", "-std=c++11", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}",
"-I", "/usr/local/include",
"-I", "/usr/local/include/opencv",
"-I", "/usr/local/include/opencv2",
"-L", "/usr/local/lib",
"-l", "opencv_aruco",
"-l", "opencv_bgsegm",
"-l", "opencv_bioinspired",
"-l", "opencv_calib3d",
"-l", "opencv_ccalib",
"-l", "opencv_core",
"-l", "opencv_datasets",
"-l", "opencv_dnn_objdetect",
"-l", "opencv_dnn",
"-l", "opencv_dpm",
"-l", "opencv_face",
"-l", "opencv_features2d",
"-l", "opencv_flann",
"-l", "opencv_freetype",
"-l", "opencv_fuzzy",
"-l", "opencv_hfs",
"-l", "opencv_highgui",
"-l", "opencv_imgcodecs",
"-l", "opencv_img_hash",
"-l", "opencv_imgproc",
"-l", "opencv_line_descriptor",
"-l", "opencv_ml",
"-l", "opencv_objdetect",
"-l", "opencv_optflow",
"-l", "opencv_phase_unwrapping",
"-l", "opencv_photo",
"-l", "opencv_plot",
"-l", "opencv_reg",
"-l", "opencv_rgbd",
"-l", "opencv_saliency",
"-l", "opencv_shape",
"-l", "opencv_stereo",
"-l", "opencv_stitching",
"-l", "opencv_structured_light",
"-l", "opencv_superres",
"-l", "opencv_surface_matching",
"-l", "opencv_text",
"-l", "opencv_tracking",
"-l", "opencv_videoio",
"-l", "opencv_video",
"-l", "opencv_videostab",
"-l", "opencv_xfeatures2d",
"-l", "opencv_ximgproc",
"-l", "opencv_xobjdetect",
"-l", "opencv_xphoto",
],
其中:“-I”指令后面的头文件坐在路径,“-L”后面是库文件所在路径,“-l” 后面是库的名字。
注意:此处有个坑就是,库的名字一定要按照上述文档中的方式写,即“opencv_XXX”,而不能按照传统上linux的库名“libopencv_XXX”,否则编译会报错说找不到函数库。
step 5. 回到测试文件,F5运行如下
但是似乎这种方法每次新开一个工程都需要再配置一遍,后面再研究一下一劳永逸的方法吧!
本文内容参考了参考1,参考2,参考3,感谢这些博主们的分享!