在vscode中,调试c语言代码时用的launch.json文件里的"cwd": "${fileDirname}",和"cwd": "${workspaceFolder}",有什么区别啊?
时间: 2023-09-15 10:23:48 浏览: 171
"cwd": "${fileDirname}" 表示当前工作目录为打开当前代码文件所在的目录,而 "cwd": "${workspaceFolder}" 表示当前工作目录为打开工作区所在的根目录。如果你在调试过程中需要读取或写入文件,那么使用 "cwd": "${fileDirname}" 可以方便地使用相对路径,而 "cwd": "${workspaceFolder}" 可以方便地维护整个工作区的目录结构。
相关问题
vscode中c语言添加launch.json
### 配置 VSCode 进行 C 语言项目的调试
为了使 Visual Studio Code 能够成功调试 C 语言项目,确保已安装 Microsoft 提供的 C/C++ 扩展,并设置好相应的编译器与调试工具(比如 GDB)[^2]。
#### 创建 `launch.json` 文件
在 `.vscode/` 目录下创建名为 `launch.json` 的文件。此文件定义了启动配置项,允许开发者指定如何构建以及运行程序。对于 C 语言来说,典型的配置如下:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/your_program_name.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build your program task name",
"miDebuggerPath": "/path/to/gdb"
}
]
}
```
上述 JSON 片段中的 `"program"` 字段应指向生成的目标可执行文件的位置;而 `"preLaunchTask"` 则关联至用于编译源码的任务名称,在实际应用时需替换为具体值[^1]。
#### 设置任务来编译代码
同样位于 `.vscode/` 下面建立另一个叫做 `tasks.json` 的文件,用来描述怎样通过命令行调用 GCC 或者其他任何支持的语言处理器来进行编译工作:
```json
{
"version": "2.0.0",
"tasks": [
{
"label": "gcc build active file",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Generated task gcc"
}
]
}
```
这段配置会告诉编辑器当触发构建操作时应该做什么——这里是以当前打开的那个 .c 文件作为输入并尝试将其转换成同名但带有 .exe 后缀的形式保存下来[^3]。
完成以上两步之后就可以正常地利用断点等功能对 C 应用实施单步跟踪等常规调试活动了。
vscode配置c语言环境launch.json
在VSCode中配置C语言环境需要配置launch.json文件,步骤如下:
1. 打开VSCode,打开C语言文件夹,选择一个C文件。
2. 点击左侧菜单栏的Debug按钮,然后点击鼠标右键,选择Add Configuration。
3. 选择C++(GDB/LLDB),点击OK。
4. 打开launch.json文件,配置如下:
```json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: gcc build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"preLaunchTask": "gcc build active file"
}
]
}
```
其中,"program"配置项为当前文件的路径,"MIMode"配置项为gdb,"miDebuggerPath"配置项为gdb的安装路径。
5. 配置完毕后,点击左侧菜单栏的Run按钮,即可开始调试C语言代码。
注意:在使用VSCode调试C语言代码时,需要先安装gcc和gdb环境。
阅读全文
相关推荐















