visual studio code调试c语言
1、打开vscode
ctrl+shift+x打开商店
搜索C/C++
1.jpg
安装、重启vscode
2、安装GCC
下载minGW
下载后解压到:
D:\Program Files (x86)\mingw64-8.1.0
然后把“D:\Program Files (x86)\mingw64-8.1.0\bin”目录添加到windows系统环境变量中
3、配置智能提示
编写一个测试文件hello.c,点击引入的标准库下的“提示灯”,生成c_cpp_properties.json文件
编辑c_cpp_properties.json
2.jpg
c_cpp_properties.json修改内容如下:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "8.1",
"compilerPath": "D:\\Program Files (x86)\\mingw64-8.1.0\\bin\\gcc.exe",// 自己电脑上的gcc路径
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
4、开启调试功能
F5或Ctrl+F5启用调试,选择C++(GDB/LLDB)生成launch.json文件,修改如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Program Files (x86)\\mingw64-8.1.0\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
5、Ctrl+Shift+P输入Tasks:Configure Task配置任务,选择使用模块创建task.json文件,选择Others模板,生成task.json文件,修改如下:
{
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc.exe build active file",
"command": "D:\\Program Files (x86)\\mingw64-8.1.0\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\Program Files (x86)\\mingw64-8.1.0\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
6、F5调试程序
代码如下:
#include
void main()
{
int i, j; //r代表行,h代表列
for(i = 1; i <= 9; i++ )
{
for(j = 1; j <= i; j++)
printf("%d * %d = %2d ", i, j, i*j);
printf("\n");
}
return 0;
}
结果如下:
PS D:\code\learning-code-accumulation\c-cpp> ^C
PS D:\code\learning-code-accumulation\c-cpp> & 'c:\Users\yl\.vscode-insiders\extensions\ms-vscode.cpptools-1.0.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-Launcher.exe' '--stdin=Microsoft-MIEngine-In-e34fv0yw.nak' '--stdout=Microsoft-MIEngine-Out-rbwz1h1i.sab' '--stderr=Microsoft-MIEngine-Error-egqdloh.lgq' '--dbgExe=D:\Program Files (x86)\mi3qgnx.3lw' '--pid=Microsoft-MIEngine-Pid-qmyvdloh.lgq' '--dbgExe=D:\Program Files (x86)\mingw64-8.1.0\bin\gdb.exe' '--interpreter=mi'
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
PS D:\code\learning-code-accumulation\c-cpp>