VSCode C_Cpp Debug

Intro

Hello,world!

​ 今天阅读Configure launch.json for C/C++ debugging in Visual Studio Code,初步完成 VSCode C/C++ debug 的配置,于是写下本文,旨在记录个人在 Windows/WSL 平台,VSCode 配置 C/C++的 Debug 环境过程,以供其他初学者参考。在此之前,你需要先安装 VSCode,配置好 WSL 环境以及 gcc 环境变量。
文章介绍了launch.jsontasks.json中部分attribute的含义,并给出本人的配置供读者参考

Steps

launch.json

配置 debug 环境,首先要配置launch.jsonlaunch.json是用来配置 VSCode 的 debug 环境的,它包含了一系列的调试配置,每个调试配置都是一个对象,包含了一系列的字段,用于定义如何启动和运行调试器。在 VSCode 中,你可以通过F5快捷键或者点击左侧的 debug 按钮进入 debug 模式,此时 VSCode 会自动寻找当前工作目录下的launch.json文件,如果没有找到,VSCode 会提示你创建一个launch.json文件,如果找到了,VSCode 会自动加载launch.json中的配置,然后根据配置启动调试器。

A launch.json file is used to configure the debugger in Visual Studio Code.

Visual Studio Code generates a launch.json (under a .vscode folder in your project) with almost all of the required information. To get started with debugging you need to fill in the program field with the path to the executable you plan to debug. This must be specified for both the launch and attach (if you plan to attach to a running instance at any point) configurations.

Launch.json Code

个人配置如下,仅供参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"version": "0.2.0",
"configurations": [
{
"name": "c/c++ gdb",
"type": "lldb",
"request": "launch",
"args": [],
"cwd": "${fileDirname}",
"console": "integratedTerminal",
"windows": {
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"preLaunchTask": "compile c file(Windows)"
},
"linux": {
"program": "${fileDirname}/${fileBasenameNoExtension}.out",
"preLaunchTask": "compile c file(Linux)"
}
}
]
}

Attributes

  • version:version of this file format
    不必修改

  • configurations:List of configurations.
    configuration 数组, 包含了你的调试配置。每个调试配置都是一个对象,包含了一系列的字段,用于定义如何启动和运行调试器

  • type:Type of configuration
    用于指定要使用的调试器类型。可选的值取决于你已经安装的调试器扩展。常见的 type 字段值有 node、java、lldb(主要用于调试c/cpp以及其他llvm支持的语言)、chrome(JavaScript,TypeScript)等等

  • name:Name of configuration; appears in the launch configuration dropdown menu.
    字面意思,此处的c/c++ gdb即为launch.jsonname

configure name
  • preLaunchTask:Task to run before debug session starts.
    在 debug 开始前执行的 task,例如编译你的代码,生成一个可执行文件(.exe, .out e.g.),如果 task 失败,则不会启动 debug session。此处我们要配置 c/cpp 的 debug 环境,所以 preLaunchTask 设置为 build active file(名字任意),与tasks.json中的 task 对象的 label 属性对应即可

  • program:Path to the program to debug.
    Specify debug program’s path。以 windows 平台为例,我们编写了 hello.c,要调试的程序即为filePath/hello.exe,我们可以用如下预定义变量简化 program

    Common pre-defined variables provided by VSCode for configuration files:

    • ${workspaceFolder}: The path of the folder opened in VS Code.
    • ${workspaceRootFolderName}: The name of the folder opened in VS Code without any slashes (/).
    • ${file}: The current opened file.
    • ${fileWorkspaceFolder}: The current opened file’s workspace folder.
    • ${relativeFile}: The current opened file relative to workspaceFolder.
    • ${relativeFileDirname}: The current opened file’s dirname relative to workspaceFolder.
    • ${fileBasename}: The current opened file’s basename.
    • ${fileBasenameNoExtension}: The current opened file’s basename with no file extension.
    • ${fileDirname}: The current opened file’s dirname.
    • ${fileExtname}: The current opened file’s extension.
    • ${lineNumber}: The current selected line number in the active file.
    • ${selectedText}: The current selected text in the active file.
    • ${execPath}: The location of the VS Code executable.
    • ${defaultBuildTask}: The name of the default build task.
  • request:Indicates whether the configuration section is intended to launch the program or attach to an already running instance.
    一般选择 launch,前端 debug 可能会用到 attach

  • console:Terminal type to use.
    取决个人喜好,可选参数如下

    • internalConsole:VSCode Panel 中的 TERMINAL

    • integratedTerminal:VSCode Panel 中的 DEBUG CONSOLE

    • externalTerminal:外部的集成终端

  • cwd:Program working directory.
    cwd 字段用于设置当前工作目录(Current Working Directory)。当你启动调试器时,这个目录将被用作程序的工作目录,一般配置为 "cwd": "${fileDirname}"

  • window/linux/osx:specific launch configuration attributes
    针对 Window,Linux/WSL,MacOS 平台单独设置的 attributes

tasks.json

debugger 正常工作,需要先编译你的代码,生成一个可执行文件(.exe, .out e.g.),于是在preLaunch中我们定义了两个 task,用来编译 c 文件
[[tasks.json的使用]]->See more

Tasks.json Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "compile c file(Windows)",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
]
},
{
"type": "shell",
"label": "compile c file(Linux)",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
]
}
]
}

Attribute

  • tasks:The task configurations. Usually these are enrichments of task already defined in the external task runner.
    例如之前在launch.json定义的两个task

    1
    2
    3
    4
    5
    6
    7
    8
    "windows": {
    "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
    "preLaunchTask": "build active file(Windows)"
    },
    "linux": {
    "program": "${fileDirname}/${fileBasenameNoExtension}.out",
    "preLaunchTask": "build active file(Linux)"
    }
  • type:Defines whether the task is run as a process or as a command inside a shell.
    可选参数有 shell,progess。gcc 命令在 shell 中执行,此处选择 shell 即可

  • label:The task’s user interface label
    任务的名称,在launch.json通过 label specify tasks to run

  • command:The command to be executed. Can be an external program or a shell command.
    此处选择 gcc 即可,也可选 clang 等 c/cpp 编译器

  • args:Arguments passed to the command when this task is invoked.
    命令的参数,例如-o,-g 等等

Outro

​ 以经典的hello.c测试,能正常 debug。希望本文对你有帮助 🕊️

demo

Author

Efterklang

Posted on

2024-01-15

Updated on

2024-09-18

Licensed under

Comments