vs code官网教程

Debug

debug至少需要两个json文件:task.json、launch.json;(为了debug而准备得)

task.json

tasks.json 定义一组任务。其中我们需要的是编译任务,通常编译一个项目使用的动词是 build。比如 dotnet build 命令就是这样的动词。
于是定义一个名字为 build 的任务,对应 label 标签。commandargs 对应我们在命令行中编译一个项目时使用的命令行和参数。typeprocess 表示此任务是启动一个进程。
例如:

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "shell",
  6. "label": "C/C++: g++.exe build active file",
  7. "command": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
  8. "args": [
  9. "-g",
  10. "${file}",
  11. "-o",
  12. "${fileDirname}\\${fileBasenameNoExtension}.exe"
  13. ],
  14. "options": {
  15. "cwd": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin"
  16. },
  17. "problemMatcher": [
  18. "$gcc"
  19. ],
  20. "group": {
  21. "kind": "build",
  22. "isDefault": true
  23. }
  24. }
  25. ]
  26. }

其中的command和args也就是对应我们在命令行输入的命令!
当为多文件需要编译时:"${file}"可以换成"${workspaceFolder}\\*.cpp",但是注意路径里面文件夹名称不要有汉字或者空格,否则会报一些奇怪得错误!

launch.json

launch.json中通常配置两个启动配置,一个是启动调试,一个是附加调试。
其中的type表示调试类型,preLaunchTask 表示在此启动开始之前需要执行的任务,这里指定的 _build_ 跟前面的 _build_ 任务就关联起来了program 是调试的程序路径,console 指定调试控制台使用内部控制台。

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "g++.exe - 生成和调试活动文件",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
  12. "args": [],
  13. "stopAtEntry": false,
  14. "cwd": "${workspaceFolder}",
  15. "environment": [],
  16. "externalConsole": false,
  17. "MIMode": "gdb",
  18. "miDebuggerPath": "D:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
  19. "setupCommands": [
  20. {
  21. "description": "为 gdb 启用整齐打印",
  22. "text": "-enable-pretty-printing",
  23. "ignoreFailures": true
  24. }
  25. ],
  26. "preLaunchTask": "C/C++: g++.exe build active file"
  27. }
  28. ]
  29. }

如果安装了多个版本的 MinGW,那么编译文件的g++和调试文件的GDB版本需要对应!不然会报错!

设置

setting.json

和file -> preference -> setting对应。

cmake和mingw32-make编译

VS Code下 cmake 配置C++环境(windows ) cmakelist编写等

  1. 编写CMakeLists.txt文件;

例如下面包含 OpenCV 库的MakeLists.txt文件编写:链接

  1. cmd中输入cmake -G "MinGW Makefiles" .产生makefile文件;
  2. 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

  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "type": "shell",
  6. "label": "C/C++: g++.exe build active file",
  7. "command": "g++",
  8. "args": [
  9. "-g",
  10. "${workspaceFolder}/*.cpp",
  11. "-o",
  12. "${workspaceFolder}/main.exe",
  13. "-I", "D:/OpenCV/opencv/build/x64/MinGW/install/include",
  14. "-I", "D:/OpenCV/opencv/build/x64/MinGW/install/include/opencv2",
  15. "-L", "D:/OpenCV/opencv/build/x64/MinGW/lib",
  16. "-l", "libopencv_calib3d450", // 后三位数字为opencv对应版本4.5.0
  17. "-l", "libopencv_core450",
  18. "-l", "libopencv_dnn450",
  19. "-l", "libopencv_features2d450",
  20. "-l", "libopencv_flann450",
  21. "-l", "libopencv_gapi450",
  22. "-l", "libopencv_highgui450",
  23. "-l", "libopencv_imgcodecs450",
  24. "-l", "libopencv_imgproc450",
  25. "-l", "libopencv_ml450",
  26. "-l", "libopencv_objdetect450",
  27. "-l", "libopencv_photo450",
  28. "-l", "libopencv_stitching450",
  29. "-l", "libopencv_video450",
  30. "-l", "libopencv_videoio450", // 不加这个处理视频时VideoCapture会报错
  31. ],
  32. "options": {
  33. "cwd": "D:/MinGW/bin"
  34. },
  35. "problemMatcher": [
  36. "$gcc"
  37. ],
  38. "group": {
  39. "kind": "build",
  40. "isDefault": true
  41. }
  42. }
  43. ]
  44. }

关键在于 OpenCV 链接

c_cpp_properties.json

  1. {
  2. "configurations": [
  3. {
  4. "name": "Win32",
  5. "includePath": [
  6. "${workspaceFolder}/**",
  7. "E:/410 Projects/Self Study/C++_Codes/test",
  8. "D:/OpenCV/opencv/build/x64/MinGW/install/include",
  9. "D:/OpenCV/opencv/build/x64/MinGW/install/include/opencv2"
  10. ],
  11. "defines": [
  12. "_DEBUG",
  13. "UNICODE",
  14. "_UNICODE"
  15. ],
  16. "compilerPath": "D:/MinGW/bin/g++.exe",
  17. "cStandard": "c11",
  18. "cppStandard": "gnu++14",
  19. "intelliSenseMode": "gcc-x64"
  20. }
  21. ],
  22. "version": 4
  23. }

includePath 里面加入 OpenCV 路径