Skip to content
| Marketplace
Sign in
Visual Studio Code>Other>Launch VariablesNew to Visual Studio Code? Get it now.
Launch Variables

Launch Variables

Little-Leaves

| (0) | Free
Use custom var like ${variables:xxx} or commands like $(shell which gdb) in launch.json
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Launch Variables

在 launch.json 中使用变量和函数。

变量

在 variables 中声明变量后,可在调试配置中使用 ${variable:id} 引用

{
    "variables": {
        "buildDir": "${workspaceFolder}/build",
    },
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Launch cpp program",
            "program": "${variable:buildDir}/executable",
        }
    ]
}

函数

使用 $(func args) 语法调用插件提供的内置函数

{
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Launch cpp program",
            "program": "${workspaceFolder}/executable",
            "miDebuggerPath": "$(shell which gdb)",
        }
    ]
}

shell 函数

语法: $(shell cmd) 将命令输出的字符串作为值进行替换。

shell 命令的运行特性:

  • 运行路径默认为当前工作目录,可通过 $(shell cd bin && python ./test.py) 的方式切换启动路径
  • 输出为多行时,会使用空格替换换行符

glob 函数

语法: $(glob [-options] <pattern>) 使用 Linux 路径通配规则匹配文件。

options:

  • -f: 只匹配文件
  • -d: 只匹配路径
  • -tg: 匹配多个文件时,选择时间戳最大的文件
  • -tl: 匹配多个文件时,选择时间戳最小的文件
  • -ng: 匹配多个文件时,选择按文件名进行字符串比较最大的文件
  • -nl: 匹配多个文件时,选择按文件名进行字符串比较最小的文件

举例:

{
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "name": "Launch cpp program",
            "program": "${workspaceFolder}/executable",
            "coreDumpPath": "$(glob -f -tg ${workspaceFolder}/*.core.*)"
        }
    ]
}

自动匹配工作目录下最新的 core dump 文件

不指定选项且匹配了多个文件时,会弹出所有选项供用户选择

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft