首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将VSCode中的C/C++配置与任务和启动配置结合起来?

如何将VSCode中的C/C++配置与任务和启动配置结合起来?
EN

Stack Overflow用户
提问于 2020-05-08 17:56:08
回答 1查看 1.3K关注 0票数 4

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

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

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

代码语言:javascript
复制
{
    "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"
        }
    ]
}

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

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

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

不幸的是,通信似乎停止了;特别是当我想添加调试目标时:

代码语言:javascript
复制
{
    "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)中添加特定主机的配置,但我需要的实际上是特定于目标的部分。

还有其他我忽略的方法吗?我使用的所有其他工具都以某种方式使调试(和构建)变量依赖于当前选定的配置。当然,我可以添加许多启动配置,但这并不是非常方便用户使用(例如,当前选择的配置与启动配置可能不匹配)。

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

代码语言:javascript
复制
{
    "configurations": [
        {
            "type": "cppdbg",
            "request": "launch",
            "program": "${cpptools.currentConfiguration.OutputName}",
        }
    ]
}

或者:

代码语言:javascript
复制
{
    "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"
            },
        }
    ]
}
EN

回答 1

Stack Overflow用户

发布于 2020-06-16 08:29:50

微软好心地接受了一个拉请求来允许这一点。在下一个版本中,可以将customConfigurationVariables存储在属性文件中,并使用launch.jsontasks.json命令查询它们。

代码语言:javascript
复制
{
    "configurations": [
        {
            "name": "Debug",
            "customConfigurationVariables": {
                "OutDir": "${workspaceFolder}/Debug"
            }
        },
        {
            "name": "Release",
            "customConfigurationVariables": {
                "OutDir": "${workspaceFolder}/Release"
            }
        }
    ],
    "version": 4
}

在发射(或任务)中:

代码语言:javascript
复制
{
    "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"
        }
    ]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61685124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档