Reputation: 81
In general, projects have several build flavors (especially for development) like Debug and Release, and some projects also allow cross-platform configurations. Visual Studio calls that "platform" (Win32/x&4...) and "configuration" for instance, and in QtCreator it's a combination of Kit (toolchain) and Configuration (debug/release), while the VSCode C++ extension calls this Configuration.
My issue is that while it is possible to query the currently selected configuration through a command, I can't find a way to use configuration-specific variables in the tasks.json or launch.json file. I can only query the configuration name.
As an example, this is a sample C/C++ properties files:
{
"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"
}
]
}
This allows me to choose between the configurations and get perfect code completion. For building, I can depend on the configuration name:
{
"tasks": [
{
"label": "build",
"type": "process",
"command": ["/usr/bin/python3"],
"args": ["waf", "--tests", "build:${command:cpptools.activeConfigName}"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
So whenever I type Ctrl+Shift+B
it will automatically build the current configuration, since the task will pass the current configuration to the build system. For that, the configuration HAS to match the build system's configuration name though.
Unfortunately, communication seems to stop there; especially when I want to add a debug target:
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "???? configuration dependent path",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"MIMode": "gdb (actually also configuration dependent)",
}
]
}
I can't seem to be able to retrieve the executable name from the configuration in any way; I have noticed one can add host-specific configurations in the launch.json file (linux, osx, windows) but what I'd need is actually target-specific sections.
Is there any other way that I overlooked? all other tools I use make debugging (and building) variables somehow dependent on the currently selected configuration. I could of course add many launch configurations but that would not be very user friendly (there could be a mismatch between the currently selected configuration and the launch configuration for instance)
Ideally, the launch configuration would look something like this:
{
"configurations": [
{
"type": "cppdbg",
"request": "launch",
"program": "${cpptools.currentConfiguration.OutputName}",
}
]
}
or:
{
"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"
},
}
]
}
Upvotes: 5
Views: 2363
Reputation: 81
Microsoft kindly accepted a pull request to allow this. In the next release, it will possible to store customConfigurationVariables
in the properties files and query them in launch.json
or tasks.json
using the cpptools.activeConfigCustomVariable
command
{
"configurations": [
{
"name": "Debug",
"customConfigurationVariables": {
"OutDir": "${workspaceFolder}/Debug"
}
},
{
"name": "Release",
"customConfigurationVariables": {
"OutDir": "${workspaceFolder}/Release"
}
}
],
"version": 4
}
and in launch (or tasks):
{
"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"
}
]
}
Upvotes: 3