Nic*_*ier 5 c++ visual-studio-code
一般来说,项目有多种构建风格(尤其是开发),例如调试和发布,并且某些项目还允许跨平台配置。例如,Visual Studio 称之为“平台”(Win32/x&4...)和“配置”,在 QtCreator 中,它是 Kit(工具链)和 Configuration(调试/发布)的组合,而 VSCode C++ 扩展则调用此 Configuration。
我的问题是,虽然可以通过命令查询当前选择的配置,但我找不到在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"
}
]
}
Run Code Online (Sandbox Code Playgroud)
这使我能够在配置之间进行选择并获得完美的代码完成。对于构建,我可以依赖配置名称:
{
"tasks": [
{
"label": "build",
"type": "process",
"command": ["/usr/bin/python3"],
"args": ["waf", "--tests", "build:${command:cpptools.activeConfigName}"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
因此,每当我键入时,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)",
}
]
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法以任何方式从配置中检索可执行文件名称;我注意到可以在 launch.json 文件(linux、osx、windows)中添加特定于主机的配置,但我需要的实际上是特定于目标的部分。
还有其他我忽略的方法吗?我使用的所有其他工具都使调试(和构建)变量在某种程度上依赖于当前选择的配置。我当然可以添加许多启动配置,但这对用户不太友好(例如,当前选择的配置和启动配置之间可能不匹配)
理想情况下,启动配置应如下所示:
{
"configurations": [
{
"type": "cppdbg",
"request": "launch",
"program": "${cpptools.currentConfiguration.OutputName}",
}
]
}
Run Code Online (Sandbox Code Playgroud)
或者:
{
"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"
},
}
]
}
Run Code Online (Sandbox Code Playgroud)
微软善意地接受了拉取请求以允许这样做。在下一个版本中,将可以存储customConfigurationVariables在属性文件中并launch.json使用tasks.json命令cpptools.activeConfigCustomVariable查询它们
{
"configurations": [
{
"name": "Debug",
"customConfigurationVariables": {
"OutDir": "${workspaceFolder}/Debug"
}
},
{
"name": "Release",
"customConfigurationVariables": {
"OutDir": "${workspaceFolder}/Release"
}
}
],
"version": 4
}
Run Code Online (Sandbox Code Playgroud)
并在启动(或任务)中:
{
"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"
}
]
}
Run Code Online (Sandbox Code Playgroud)