前言
?? 本文包括VScode安装、C++环境配置以及OpenCV配置全过程。VS Studio配置OpenCV比较简单,可以直接使用OpenCV官网已有的用VS Studio编译器编译好的OpenCV库。但VS Code不能直接利用VS Studio编译好的OpenCV库,因此需要借助MinGW-64和CMake工具对OpenCV源码重新进行编译(编译前提是C:\Users目录下不能包含中文,否则无法成功编译)。
一、资源下载
1、VS Code
?百度网盘是我自己下载的版本,与官网最新版本不一定一致。 ?? 1)百度网盘下载:VSCodeSetup-x64-1.66.2.exe (提取码:ggec) ?? 2)官网下载:Visual Studio Code(官网下载速度很慢,可以安装IDM,使用方法可参考下载外网资源加速) 
2、MinGW-w64
?? 1)百度网盘下载:x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z (提取码:2dis) ?? 2)官网下载:MinGW-w64离线包下载地址(一定要是"seh"后缀的,千万别错了!!!) 
3、CMake
?? 1)百度网盘下载:cmake-3.9.0-rc3-win64-x64 .zip (提取码:zgqi) ?? 2)官网下载:CMake(要选择二进制文件,即Binary distributions栏目) 
4、OpenCV
?? 1)百度网盘下载:opencv-4.5.5-vc14_vc15.exe (提取码:0hf7) ?? 2)官网下载:OpenCV 
二、环境配置
1、VS Code安装
??双击.exe文件开始安装,同意此协议,点击下一步:  ??选择安装路径,点击下一步:  ??点击下一步:  ??根据图中提示勾选,点击下一步:  ??点击安装: 
2、VS Code的C++配置
??选择喜欢的界面形式,点击Mark Done:  ??按图中所示步骤安装C/C++、C/C++ Extension Pack、C/C++ Intellisense、Code Runner插件:  ??桌面新建一个test文件夹,再在VS Code界面点击Open Folder打开文件夹,进行简单测试:  ??选择trust:  ??点击左上角File --> New File --> 按ctrl+s保存文件名为hello.cpp,写一个测试代码:
#include<iostream>
using namespace std;
int main(){
cout<<"Hello,VScode!"<<endl;
return 0;
}
??点击右上角三角形进行编译:  ??这个地方报错,是因为VS Code只是一个编辑器,如果想正常运行C++程序还需要安装编译器。
3、MinGW-w64安装
??解压MinGW-w64压缩包,存在下图文件夹:  ??将该文件夹的bin路径添加到环境变量中: ??检查MinGW-w64是否安装成功,若成功则如下图所示:  ??重启VS Code,再次运行程序成功: 
4、OpenCV配置
??将opencv-4.5.5-vc14_vc15.exe解压到除C盘外的其他盘内(若是C盘,cmake 时很可能报错)  ??在 opencv\build\x64 路径下新建一个文件夹(可自起,这里是 MinGW ) 
5、CMake安装
??将cmake-3.9.0-rc3-win64-x64解压,文件夹如图所示:  ??将该文件夹下的bin文件路径添加到环境变量:  ??检查cmake是否安装成功: 
6、生成MakeFiles
??进入D:\cmake-3.9.0-rc3-win64-x64\bin,先不要进行任何操作:  ??在操作cmake-gui之前,如果没有翻过墙的话,先用记事本方式打开C:\Windows\System32\drivers\etc\hosts,在文件的末尾加上151.101.72.133 raw.githubusercontent.com(因为cmake-gui会下载一些文件, 这些文件都在raw.githubusercontent.com上, 如果不修改一下hosts,许多文件很可能会下载失败)。(如果hosts文件修改后无法保存,则是因为电脑默认hosts权限为只读,在属性里修改hosts文件为可写即可成功保存,记得环境配置好之后把hosts属性改回去,以保护电脑安全性)。   ??打开cmake-gui,选择OpenCV的源文件路径和MakeFiles保存路径(即之前新建的MinGW):  ??点击Configure,弹窗配置如下,点击Next:  ??选择前面安装的D:/mingw64/bin文件夹下的gcc.exe和g++.exe,点击Finsh  ??执行过程中消息框会出现一堆红色信息,最后显示Configure done,是正常的。显示Configure done后,勾选BUILD_ opencv_world、WITH_ OPENGL和BUILD EXAMPLES,不勾选WITH_IPP、WITH_MSMF和ENABLE_PRECOMPILED_HEADERS (如果有的话),CPU_ DISPATCH选空。再次点击Configure,这时会发现没有红色错误信息了。显示Configure done之后再点击Generate,会显示Generate Done: 
7、编译OpenCV
??cmd到MakeFiles所在文件夹,执行如下命令:
minGW32-make -j 8
 ??如果报错可查阅编译报错解答,如果编译成功则执行如下命令完成装载:
mingw32-make install
 ??将D:\opencv\build\x64\MinGW\bin加入环境变量: 
8、VS Code配置
(1)launch.json配置
   ??在生成的launch.json文件中复制一下代码(需要配置miDebuggerPath项,注意修改name、preLaunchTask的 opencv版本号):
{
"version": "0.2.0",
"configurations": [
{
"name": "opencv4.5.5 debuge",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/mingw64/bin/gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "opencv4.5.5 compile task"
}
]
}
(2)c_cpp_properties.json配置
??按快捷键crtl+shift+p,出现如下弹窗: ??点击C/C++:Edit Confighrations(UI)进行如下配置: 
  ??配置完成会自动生成c_cpp_properties.json文件: 
(3)task.json配置
??点击Terminal的Configure Tasks:  ??出现如下弹窗,选择C/C++:g++.exe生成活动文件  ??生成task.json文件: 
(4)settings.json配置
??按ctrl+, 出现如下弹窗,输入code-runner:Executor Map,点击下方的 Edit in settings.json  点击 Edit in settings.json之后会生成settigs.json(该文件地址是:C:\Users\WCJ\AppData\Roaming\Code\User\settings.json),在生成的settings.json中复制如下代码:
{
"workbench.colorTheme": "Default Light+",
"editor.suggest.snippetsPreventQuickSuggestions": false,
"code-runner.runInTerminal": true,
"files.autoSave": "afterDelay",
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp":"cd $dir && g++ $fileName -o $fileNameWithoutExt -I D:/opencv/build/include -L D:/opencv/build/x64/MinGW/bin -l libopencv_world455 -l opencv_videoio_ffmpeg455_64 && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u",
"perl": "perl",
"perl6": "perl6",
"ruby": "ruby",
"go": "go run",
"lua": "lua",
"groovy": "groovy",
"powershell": "powershell -ExecutionPolicy ByPass -File",
"bat": "cmd /c",
"shellscript": "bash",
"fsharp": "fsi",
"csharp": "scriptcs",
"vbscript": "cscript //Nologo",
"typescript": "ts-node",
"coffeescript": "coffee",
"scala": "scala",
"swift": "swift",
"julia": "julia",
"crystal": "crystal",
"ocaml": "ocaml",
"r": "Rscript",
"applescript": "osascript",
"clojure": "lein exec",
"haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
"racket": "racket",
"scheme": "csi -script",
"ahk": "autohotkey",
"autoit": "autoit3",
"dart": "dart",
"pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
"d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
"haskell": "runhaskell",
"nim": "nim compile --verbosity:0 --hints:off --run",
"lisp": "sbcl --script",
"kit": "kitc --run",
"v": "v run",
"sass": "sass --style expanded",
"scss": "scss --style expanded",
"less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
"FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
},
"window.zoomLevel": 1,
"files.associations": {
"iostream": "cpp",
"ostream": "cpp"
}
}
三、测试案例
1、测试代码
#include <iostream>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
string path = "C:/Users/WCJ/Desktop/0402/01/0.bmp";
cv::Mat img = imread(path);
imshow("img",img);
waitKey(0);
return 0;
}
2、测试结果

|