https://www.bilibili.com/video/BV1q8411z7xU?p=13&vd_source=a302e304b0aa60652c390b422ff81ab8
编译执行:tasks.json
调试程序:launch.json
多个程序编译若不配置会出现以下错误
the prelaunchTask ‘C/C++:g++.exe build active file’ terminated with exit code -1
解决方案——配置tasks.json文件
1
2
由1变成2即可解决
若程序调试不进行配置则会出现以下错误
launch:program ‘c:\Users…\build\Debug\outDebug’ does not exit
解决方案——配置launch.json文件
单文件
创建Add.c
#include <stdio.h>
int Add(int x, int y)
{
return x + y;
}
int main()
{
int a =10;
int b = 20;
int c = Add(a,b);
printf("a + b = %d\n",c);
return 0;
}
Configure Tasks
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",//需要编译的文件,file单个文件
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",//输出的文件的名字是"${fileDirname}\\${fileBasenameNoExtension}.exe"
""
],
"options": {
"cwd": "C:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/gcc.exe"
}
]
}
将单个文件编译成可执行文件.exe文件。
在终端中执行即可。
多文件
Add.c
#include <stdio.h>
int Add(int x, int y)
{
return x + y;
}
Add.h
int Add(int x, int y);
test.c
#include <stdio.h>
#include "Add.h"
int main()
{
int a = 10;
int b =50;
int sum = Add(a,b);
printf("%d\n",sum);
}
执行结果
重点配置tasks.json文件
c++配置
1、编辑配置
ctrl+shift+p调出编辑配置选项
为了能够编译C++,可以添加g++编译器
指定代码提示工具gcc-x64(legacy)
最终在c_cpp_properties.json生成编译选项如下所示
2、生成C++源文件编译的tasks信息
生成编译配置
vscode调试代码——launch.json文件
launch.json
launch.json需要修改的地方两处:
1、“program”:
2、“miDebuggerPath”:
由tasks.json生成的是这个文件,那么launch.json也是调试这个文件。
调试器gdb的路径
将gdb路径添加上,注意需要增加****
最终修改launch.json如下
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": true,
"cwd": "c:/Users/rsd/Desktop/qizi",
"program": "${workspaceFolder}\\${workspaceRootFolderName}.exe",
"MIMode": "gdb",
"miDebuggerPath": "C:\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
最终可调试