一般来说,项目有几种构建风格(特别是对于开发),比如调试和发布,有些项目还允许跨平台配置。Visual将其称为“平台”(Win32 32/x&4.)例如,“配置”,在QtCreator中,它是Kit (工具链)和配置(调试/发布)的组合,而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)中添加特定主机的配置,但我需要的实际上是特定于目标的部分。
还有其他我忽略的方法吗?我使用的所有其他工具都以某种方式使调试(和构建)变量依赖于当前选定的配置。当然,我可以添加许多启动配置,但这并不是非常方便用户使用(例如,当前选择的配置与启动配置可能不匹配)。
理想情况下,启动配置如下所示:
{
"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"
},
}
]
}发布于 2020-06-16 08:29:50
微软好心地接受了一个拉请求来允许这一点。在下一个版本中,可以将customConfigurationVariables存储在属性文件中,并使用launch.json或tasks.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"
}
]
}https://stackoverflow.com/questions/61685124
复制相似问题