Debug
debug至少需要两个json文件:task.json、launch.json;(为了debug而准备得)
task.json
tasks.json 定义一组任务。其中我们需要的是编译任务,通常编译一个项目使用的动词是 build。比如 dotnet build 命令就是这样的动词。
于是定义一个名字为 build 的任务,对应 label 标签。command 和 args 对应我们在命令行中编译一个项目时使用的命令行和参数。type 为 process 表示此任务是启动一个进程。
例如:
{"version": "2.0.0","tasks": [{"type": "shell","label": "C/C++: g++.exe build active file","command": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe","args": ["-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {"cwd": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true}}]}
其中的command和args也就是对应我们在命令行输入的命令!
当为多文件需要编译时:"${file}"可以换成"${workspaceFolder}\\*.cpp",但是注意路径里面文件夹名称不要有汉字或者空格,否则会报一些奇怪得错误!
launch.json
launch.json中通常配置两个启动配置,一个是启动调试,一个是附加调试。
其中的type表示调试类型,preLaunchTask 表示在此启动开始之前需要执行的任务,这里指定的 _build_ 跟前面的 _build_ 任务就关联起来了。program 是调试的程序路径,console 指定调试控制台使用内部控制台。
{// 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": "g++.exe - 生成和调试活动文件","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "C/C++: g++.exe build active file"}]}
如果安装了多个版本的 MinGW,那么编译文件的g++和调试文件的GDB版本需要对应!不然会报错!
设置
setting.json
和file -> preference -> setting对应。
cmake和mingw32-make编译
- 编写CMakeLists.txt文件;
例如下面包含 OpenCV 库的MakeLists.txt文件编写:链接
- cmd中输入
cmake -G "MinGW Makefiles" .产生makefile文件; - cmd中输入
mingw32-make进行编译;
注意最后还需要make才能生成可执行文件,make在安装得MinGW(Minimalist GNU for Windows适用于windows的GNU)中有,但是其名称为:mingw32-make.exe,所以在make的时候需要输入mingw32-make!
VS Code 配置 OpenCV 4.5.0
task.json
{"version": "2.0.0","tasks": [{"type": "shell","label": "C/C++: g++.exe build active file","command": "g++","args": ["-g","${workspaceFolder}/*.cpp","-o","${workspaceFolder}/main.exe","-I", "D:/OpenCV/opencv/build/x64/MinGW/install/include","-I", "D:/OpenCV/opencv/build/x64/MinGW/install/include/opencv2","-L", "D:/OpenCV/opencv/build/x64/MinGW/lib","-l", "libopencv_calib3d450", // 后三位数字为opencv对应版本4.5.0"-l", "libopencv_core450","-l", "libopencv_dnn450","-l", "libopencv_features2d450","-l", "libopencv_flann450","-l", "libopencv_gapi450","-l", "libopencv_highgui450","-l", "libopencv_imgcodecs450","-l", "libopencv_imgproc450","-l", "libopencv_ml450","-l", "libopencv_objdetect450","-l", "libopencv_photo450","-l", "libopencv_stitching450","-l", "libopencv_video450","-l", "libopencv_videoio450", // 不加这个处理视频时VideoCapture会报错],"options": {"cwd": "D:/MinGW/bin"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true}}]}
c_cpp_properties.json
{"configurations": [{"name": "Win32","includePath": ["${workspaceFolder}/**","E:/410 Projects/Self Study/C++_Codes/test","D:/OpenCV/opencv/build/x64/MinGW/install/include","D:/OpenCV/opencv/build/x64/MinGW/install/include/opencv2"],"defines": ["_DEBUG","UNICODE","_UNICODE"],"compilerPath": "D:/MinGW/bin/g++.exe","cStandard": "c11","cppStandard": "gnu++14","intelliSenseMode": "gcc-x64"}],"version": 4}
includePath 里面加入 OpenCV 路径
