注意:
本笔记的重点在于VS Code中的一些关于OpenCV配置文件的说明,而关于OpenCV的安装编译(基于MinGW)这里不再赘述,可参考网上资源。
如果想直接查看OpenCV配置文件(task.json,launch.json,c_cpp_properties.json),可直接翻到文末查看。
C++环境配置过程需要主要参考的是官方教程:https://code.visualstudio.com/docs/cpp/config-mingw
在VSCode中扩展窗口直接搜索**C++**然后安装C/C++
扩展即可,这里推荐同时安装C++ Intelligence
。
创建一个OpenCV_Project
文件夹作为VSCode工作空间,在VSCode编辑器中打开此文件夹。
在此工作空间内新建源文件testCpp.cpp
,并粘贴以下代码:
#include
using namespace std;
int main(void)
{
cout << "Testing for Cpp." << endl;
system("pause");
return 0;
}
添加task.json
以编译源文件(调用g++编译器生成可执行文件)
依次选择终端——>配置默认生成任务——>g++.exe build active file
,此时会在.vscode文件夹下生成task.json
文件,打开此文件并添加如下内容:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "ThePathTo\\mingw64\\bin\\g++.exe", //这里修改为自己的g++编译器的绝对路径
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
此时可回到testCpp.cpp文件中,按Ctrl + Shift + B
或终端——>运行构建任务
运行定义的构建任务tasks.json,生成可执行文件testCpp.exe
依次选择运行——>添加配置——>C++ (GDB/LLDB)——>g++.exe build and debug active file
;
类似地,此时会在.vscode文件夹下生成launch.json
文件,打开此文件并添加如下内容:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "ThePathTo\\mingw64\\bin\\gdb.exe", //这里修改为自己的gdb调试器的绝对路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
返回到testCpp.cpp中,可根据需要添加一些断点,点击运行——>启动调试
,查看调试结果。
基于C++的OpenCV配置需要修改两个配置文件:
task.json
:在其中中添加一些编译参数,指定OpenCV的头文件及库文件搜索路径,同时添加一些常用的静态链接库。c_cpp_properties.json
:添加C++的一些其他配置选项,如OpenCV头文件及g++编译器路径等。完整的OpenCV(C++)配置文件(task.json,launch.json,c_cpp_properties.json)内容如下:
注意:需要根据自己的MinGW及OpenCV的编译路径来对应的修改配置文件中的部分内容!!!
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++ - OpenCV", //这里的label值需要与launch.json中的preLaunchTask值相同
"command": "D:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\msys64\\mingw64\\include\\opencv4",
"-I",
"D:\\msys64\\mingw64\\include\\opencv4\\opencv2",
"-L",
"D:\\msys64\\mingw64\\lib",
"-l",
"opencv_core",
"-l",
"opencv_highgui",
"-l",
"opencv_imgproc",
"-l",
"opencv_imgcodecs"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
注意"cmd"配置选项处,这里要填写
${fileDirname}
,不然OpenCV中的cv::imshow()图像显示窗口会一闪而过(这个问题困扰了我好久- -|||)。
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}", //Not ${workspaceFolder}
"environment": [],
"externalConsole": false, //显示外部控制台窗口,可根据需要设为true
"MIMode": "gdb",
"miDebuggerPath": "D:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++ - OpenCV", //这里的preLaunchTask值需要与task.json中的label值相同
}
]
}
{
"configurations": [
{
"name": "win",
"includePath": [
"${workspaceFolder}/**",
"D:\\msys64\\mingw64\\include\\opencv4",
"D:\\msys64\\mingw64\\include\\opencv4\\opencv2"
],
"defines": [],
"compilerPath": "D:\\msys64\\mingw64\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
OK,C++ & OpenCV的配置过程告一段落。需要注意的是,前面的这些配置文件都在.vscode
文件夹下,而.vscode
文件夹又在打开的工作空间中(本文为OpenCV_Project),故以上配置仅仅在此工作空间中生效。
建议以后的OpenCV程序代码都放在总的配置好的工作空间下,或者将.vscode复制到其他要运行程序的文件夹下,避免配置失效。