VSCode 配置一键运行与 Debug

本文介绍如何在VSCode中配置CodeRunner插件,实现Python和C++的一键运行及内置终端显示。同时,详细说明了如何设置Debug配置,以便在多种语言环境下进行高效代码调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文首发于我的个人博客:Sui Xin’s Blog
原文:https://suixinblog.cn/2019/09/vscode-code-runner-debug.html
作者:Sui Xin

本文以 Python 和 C++ 为例,在 VSCode 中配置多语言一键运行和 Debug 环境。

Code Runner

插件中心搜索并安装 Code Runner,安装完成后只需简单配置即可使用。
默认使用快捷键 ⌃⌥N 来运行脚本,使用 ⌃⌥M 来结束运行。

配置多语言执行命令

在 VSCode 设置中搜索 Code Runner,找到 Code-runner: Executor Map,点击 Edit in settings.json 打开系统设置文件,添加如下设置:

"code-runner.executorMap": {
        "python": "python3 $fullFileName",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -g && $dir$fileNameWithoutExt"
    }

可根据自身需求进行更改,上述代码配置的 C++ 是编译后直接运行可执行文件的。
其中,根据官网,可使用的变量有:

  • $workspaceRoot: The path of the folder opened in VS Code
  • $dir: The directory of the code file being run
  • $dirWithoutTrailingSlash: The directory of the code file being run without a trailing slash
  • $fullFileName: The full name of the code file being run
  • $fileName: The base name of the code file being run, that is the file without the directory
  • $fileNameWithoutExt: The base name of the code file being run without its extension
  • $driveLetter: The drive letter of the code file being run (Windows only)
  • $pythonPath: The path of Python interpreter (set by Python: Select Interpreter command)

让程序在内置终端下运行

默认的 Code Runner 会在 OUTPUT 标签页下运行,在设置中将 Code-runner: Run In Terminal 打开即可修改为在内置终端下运行。

Debug

为了能够在全局下使用定义的 Debug,我们直接在 VSCode 的设置文件中设置 Debug 配置(settings.json 可在 UI 设置右上角点击打开)。
settings.json 中添加如下设置:

"launch": {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C++",
                "type": "cppdbg",
                "request": "launch",
                "targetArchitecture": "x86",
                "program": "${fileDirname}/${fileBasenameNoExtension}",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "MIMode": "lldb"
            },
            {
                "name": "Python",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal"
            }
        ]
    }

保存退出即可。默认按 F5 即可调试代码。

参考

https://code.visualstudio.com/docs/editor/debugging#_global-launch-configuration

### 配置 VSCode 中 C++ 的调试设置 为了在 VSCode 中成功配置并使用 C++ 调试功能,需完成几个关键步骤。确保已安装适用于 C/C++ 的扩展插件[^1]。 #### 创建 `launch.json` 文件用于调试配置 通过按下 Ctrl + Shift + P 打开命令面板,在其中输入 "Add Configuration..." 并选择适用的环境来初始化 `.vscode/launch.json` 文件。对于 MinGW-w64 用户而言,应选取类似于"GDB Debug (Windows)"这样的预设模板[^2]。 此 JSON 文件通常包含如下字段: - **name**: 描述该特定配置名称; - **type**: 指定使用的调试器类型(例如 cppdbg 表示 C++ Debugger); - **request**: 设置请求模式为 launch 或 attach; - **program**: 定义待执行程序路径,默认情况下指向 `${workspaceFolder}/a.exe`; - **args**: 启动参数列表; - **stopAtEntry**: 控制是否应在入口处暂停; - **cwd**: 工作目录位置; - **environment**: 自定义环境变量数组; - **externalConsole**: 是否启用外部控制台窗口; - **MIMode**: 使用哪个 MI 兼容调试器(gdb 或 lldb),对于 Windows 上的 GCC 编译项目推荐 gdb; - **setupCommands**: 发送至 GDB/Lldb 前端的一系列指令; - **preLaunchTask**: 运行前的任务标签名,一般关联到 tasks.json 内部构建任务; ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "console": "internalConsole", "MIMode": "gdb", "miDebuggerPath": "C:\\path\\to\\mingw\\bin\\gdb.exe", // 根据实际GDB路径修改 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "Build" } ] } ``` #### 构建任务设定 (`tasks.json`) 同样地,利用快捷键组合调出命令面板并通过"Configure Tasks"选项建立或编辑现有的`.vscode/tasks.json`文件。当选择了"C/C++: g++.exe build active file"作为默认构建方式时,将会自动生成一段基础结构化的JSON代码片段[^3]。 下面是一个典型的例子展示如何指定编译器及其标志位以及其他必要的属性: ```json { "version": "2.0.0", "tasks": [ { "label": "Build", "type": "shell", "command": "g++", "args": [ "-g", "${relativeFile}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task." } ] } ``` 上述配置允许开发者轻松实现一键编译调试流程自动化,极大提高了开发效率和体验感。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值