【问题标题】:how can I tie together the C/C++ configurations in VSCode with the tasks and launch configurations?如何将 VSCode 中的 C/C++ 配置与任务和启动配置联系起来?
【发布时间】:2020-05-08 17:56:08
【问题描述】:

一般来说,项目有多种构建风格(尤其是用于开发),例如 Debug 和 Release,有些项目还允许跨平台配置。例如,Visual Studio 将其称为“平台”(Win32/x&4...)和“配置”,在 QtCreator 中它是工具包(工具链)和配置(调试/发布)的组合,而 VSCode C++ 扩展则称为此配置。

我的问题是,虽然可以通过命令查询当前选择的配置,但我找不到在 tasks.json 或 launch.json 文件中使用特定于配置的变量的方法。我只能查询配置名称。

例如,这是一个示例 C/C++ 属性文件:

{
    "configurations": [
        {
            "name": "linux_gnu_amd64-clang_amd64-10.0.0:debug",
            "compileCommands": "${workspaceFolder}/.vscode/linux_gnu_amd64-clang_amd64-10.0.0/debug/compile_commands.json",
            "intelliSenseMode": "clang-x64",
            "cStandard": "c11",
            "cppStandard": "c++17"
        },
        {
            "name": "linux_gnu_amd64-clang_amd64-10.0.0:final",
            "compileCommands": "${workspaceFolder}/.vscode/linux_gnu_amd64-clang_amd64-10.0.0/final/compile_commands.json",
            "intelliSenseMode": "clang-x64",
            "cStandard": "c11",
            "cppStandard": "c++17"
        },
        {
            "name": "linux_gnu_amd64-clang_amd64-11.0.0:debug",
            "compileCommands": "${workspaceFolder}/.vscode/linux_gnu_amd64-clang_amd64-11.0.0/debug/compile_commands.json",
            "intelliSenseMode": "clang-x64",
            "cStandard": "c11",
            "cppStandard": "c++17"
        },
        {
            "name": "linux_gnu_amd64-clang_amd64-11.0.0:final",
            "compileCommands": "${workspaceFolder}/.vscode/linux_gnu_amd64-clang_amd64-11.0.0/final/compile_commands.json",
            "intelliSenseMode": "clang-x64",
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ]
}

这允许我在配置之间进行选择并获得完美的代码完成。对于构建,我可以依赖配置名称:

{
  "tasks": [
    {
      "label": "build",
      "type": "process",
      "command": ["/usr/bin/python3"],
      "args": ["waf", "--tests", "build:${command:cpptools.activeConfigName}"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

所以每当我输入Ctrl+Shift+B 时,它都会自动构建当前配置,因为任务会将当前配置传递给构建系统。为此,配置必须与构建系统的配置名称相匹配。

不幸的是,交流似乎就此停止;特别是当我想添加调试目标时:

{
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "???? configuration dependent path",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "MIMode": "gdb (actually also configuration dependent)",
        }
    ]
}

我似乎无法以任何方式从配置中检索可执行名称;我注意到可以在 launch.json 文件(linux、osx、windows)中添加特定于主机的配置,但实际上我需要的是特定于 target 的部分。

还有其他我忽略的方法吗?我使用的所有其他工具使调试(和构建)变量以某种方式依赖于当前选择的配置。我当然可以添加许多启动配置,但这对用户来说不是很友好(例如,当前选择的配置和启动配置之间可能不匹配)

理想情况下,启动配置应如下所示:

{
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "program": "${cpptools.currentConfiguration.OutputName}",
        }
    ]
}

或:

{
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "linux_gnu_amd64-clang_amd64-10.0.0:debug": {
              "program": "path/to/clang10/debug/exe"
            },
            "linux_gnu_amd64-clang_amd64-10.0.0:final": {
              "program": "path/to/clang10/final/exe"
            },
            "linux_gnu_amd64-clang_amd64-11.0.0:debug": {
              "program": "path/to/clang11/debug/exe"
            },
            "linux_gnu_amd64-clang_amd64-11.0.0:final": {
              "program": "path/to/clang11/final/exe"
            },
        }
    ]
}

【问题讨论】:

  • 是否使用 CMake 选项? CMake 工具扩展公开了可执行文件的路径:vector-of-bool.github.io/docs/vscode-cmake-tools/debugging.html
  • 感谢提示,我的大部分项目文件实际上都是由构建系统生成的,我会检查我的构建系统是否可以生成一个虚拟的 CMakeFile。许多 IDE 与 CMake 交互良好,所以这实际上是我想研究的。我还注意到,在您发布的链接之后,CMake 团队创建了插件来解决我目前面临的问题,所以这可能意味着(还)没有直接的方法

标签: c++ visual-studio-code


【解决方案1】:

Microsoft 欣然接受了 pull request 以允许这样做。在下一个版本中,可以将customConfigurationVariables 存储在属性文件中,并使用cpptools.activeConfigCustomVariable 命令在launch.jsontasks.json 中查询它们

{
    "configurations": [
        {
            "name": "Debug",
            "customConfigurationVariables": {
                "OutDir": "${workspaceFolder}/Debug"
            }
        },
        {
            "name": "Release",
            "customConfigurationVariables": {
                "OutDir": "${workspaceFolder}/Release"
            }
        }
    ],
    "version": 4
}

在启动(或任务)中:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${input:OutDir}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb"
        }
    ],
    "inputs": [
        {
            "id": "OutDir",
            "type": "command",
            "command": "cpptools.activeConfigCustomVariable",
            "args": "OutDir"
        }
    ]
}

【讨论】:

    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-13
    相关资源
    最近更新 更多