vscode配置C/C++调试环境

发布时间 2023-05-27 18:48:33作者: 仰望星空-自然-7

1. Ctrl+Shift+P, 输入 tasks, 选择"Tasks:Configure Default Build Task", z这会生成tasks.json.然后, 修改其中的args, 删掉原来的${file}, 并将工程下的c文件添加进去, 即${fileDirname}/*c(或单个文件添加也行, 笔者因为工程的所有的c文件都在一个路径下, 所以用的*.c):

 1 {
 2     "version": "2.0.0",
 3     "tasks": [
 4         {
 5             "type": "cppbuild",
 6             "label": "C/C++: gcc 生成活动文件",
 7             "command": "/usr/bin/gcc",
 8             "args": [
 9                 "-fdiagnostics-color=always",
10                 "-g",
11                 "${fileDirname}/*.c",
12                 "-o",
13                 "${fileDirname}/${fileBasenameNoExtension}"
14             ],
15             "options": {
16                 "cwd": "${fileDirname}"
17             },
18             "problemMatcher": [
19                 "$gcc"
20             ],
21             "group": {
22                 "kind": "build",
23                 "isDefault": true
24             },
25             "detail": "编译器: /usr/bin/gcc"
26         }
27     ]
28 }

2. 在debug按钮(即左侧的那个虫子按钮)下, 点击创建launch.json, 并且点击添加配置, 选择配置中的"C/C++: (gdb) 启动", 然后, 可以自己手工添加配置参数"preLaunchTask", 该参数的值与task.json中的"label参数"的值保持一致, 以便使用OS自带的终端(需要把参数"externalConsole"的值设置为true), 这时, launch.json如下:

 1 {
 2     // 使用 IntelliSense 了解相关属性。 
 3     // 悬停以查看现有属性的描述。
 4     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
 5     "version": "0.2.0",
 6     "configurations": [
 7         {
 8             "preLaunchTask": "C/C++: gcc 生成活动文件",
 9             "name": "(gdb) 启动",
10             "type": "cppdbg",
11             "request": "launch",
12             "program": "${workspaceFolder}/${fileBasenameNoExtension}",
13             "args": [],
14             "stopAtEntry": false,
15             "cwd": "${fileDirname}",
16             "environment": [],
17             "externalConsole": true,
18             "MIMode": "gdb",
19             "setupCommands": [
20                 {
21                     "description": "为 gdb 启用整齐打印",
22                     "text": "-enable-pretty-printing",
23                     "ignoreFailures": true
24                 },
25                 {
26                     "description": "将反汇编风格设置为 Intel",
27                     "text": "-gdb-set disassembly-flavor intel",
28                     "ignoreFailures": true
29                 }
30             ]
31         }
32  
33     ]
34 }

3. Ctrl+Shift+P, 输入C/C++, 选择"C/C++: Edit Configurations(JSON)", 生成一个c_cpp_properties.json, 如下:

 1 {
 2     "configurations": [
 3         {
 4             "name": "Linux",
 5             "includePath": [
 6                 "${workspaceFolder}/**"
 7             ],
 8             "defines": [],
 9             "compilerPath": "/usr/bin/gcc",
10             "cStandard": "c17",
11             "cppStandard": "gnu++14",
12             "intelliSenseMode": "linux-gcc-x64"
13         }
14     ],
15     "version": 4
16 }