VSCode、C++和CMake -每个目标一个启动配置

p4tfgftt  于 2023-04-21  发布在  Vscode
关注(0)|答案(2)|浏览(217)

我使用Visual Studio Code和CMake extension来处理一个大项目,该项目由一些库和可执行文件组成,它们都可以作为单独的cmake构建目标。
我配置了VSCode / * tasks.json * 来使用其默认构建构建所选目标。

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "CMake build",
      "type": "cmake",
      "command": "build",
      "problemMatcher": [],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

现在,只要我点击<Ctrl-Shift-b>,所选的 * 构建目标 * 就是构建。
然后我通过创建一个 launch.json 文件来创建一个启动配置:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${command:cmake.launchTargetPath}",
      "args": ["--serialPort","/dev/ttyS0"],
      "stopAtEntry": false,
      "cwd": "${command:cmake.launchTargetDirectory}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

现在,每当我点击F5时,gdb调试器就会用所选的 launch target 启动。
这很好用,除了只有 * 一个 * 启动目标需要我提供的"args": ["--serialPort","/dev/ttyS0"]。其他一些拒绝使用给定的参数启动。
那么,有没有一种方法可以在VSCode中使用cmake为不同的目标设置不同的参数?

beq87vna

beq87vna1#

您可以使用扩展命令变量v1.17.0
如果使用命令extension.commandvariable.file.fileAsKey,则可以使用命令变量${command:cmake.launchTargetPath}获取用于将键(文件路径部分)与命令行参数匹配的路径。
选择的默认值(-v0)是一个命令选项,其他程序可以接受,但不会执行主要操作。通常选择-v来选择调试日志级别。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${command:cmake.launchTargetPath}",
      "args": ["${input:arg1}", "${input:arg2}"],
      "stopAtEntry": false,
      "cwd": "${command:cmake.launchTargetDirectory}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ],
  "inputs": [
    {
      "id": "arg1",
      "type": "command",
      "command": "extension.commandvariable.file.fileAsKey",
      "args": {
        "@useCommand": "${command:cmake.launchTargetPath}",
        "@default": "-v0",
        "proj1": "--serialPort"
      }
    },
    {
      "id": "arg2",
      "type": "command",
      "command": "extension.commandvariable.file.fileAsKey",
      "args": {
        "@useCommand": "${command:cmake.launchTargetPath}",
        "@default": "-v0",
        "proj1": "/dev/ttyS0"
      }
    }
  ]
}

编辑

在版本1.18.1中,我将调试日志添加到fileAsKey命令中。
1.将以下行添加到args属性:

"@debug": true

1.打开VSC的开发工具:帮助切换开发者工具
1.清除控制台
1.使用F5启动调试会话
在控制台中,您会发现大量文本,如果fileAsKey执行无误,您应该会发现以下消息:

[Extension Host] commandvariable.file.fileAsKey: debug logs enabled
[Extension Host] commandvariable.file.fileAsKey: use command variable: ${command:cmake.launchTargetPath}
[Extension Host] commandvariable.file.fileAsKey: execute command: cmake.launchTargetPath
[Extension Host] [CMakeTools] [debug] [extension] [6927] cmake.launchTargetPath started
[Extension Host] [CMakeTools] [debug] [cache] Reading CMake cache file C:/Projects/cmakeQuickStart/build/CMakeCache.txt
.....
[Extension Host] [CMakeTools] [debug] [extension] [6927] cmake.launchTargetPath finished (returned "C:\\Projects\\cmakeQuickStart\\build\\helloAll.exe")
[Extension Host] commandvariable.file.fileAsKey: execute command result: C:\Projects\cmakeQuickStart\build\helloAll.exe
[Extension Host] commandvariable.file.fileAsKey: path used: C:/Projects/cmakeQuickStart/build/helloAll.exe
[Extension Host] commandvariable.file.fileAsKey: default value: -v0
[Extension Host] commandvariable.file.fileAsKey: try key: helloAll
[Extension Host] commandvariable.file.fileAsKey: before variable substitution: /dev/ttyS0
[Extension Host] commandvariable.file.fileAsKey: after variable substitution: /dev/ttyS0
8tntrjer

8tntrjer2#

这可能无法解决问题,但如果您无法选择要启动的正确目标,请按照以下说明操作:尝试以下命令:CMake: set debug target并选择你想要的。要输入一个命令到Menu > View > Command Palette或使用快捷方式Shift + Ctrl + P。另一个选择是查看底部栏,不仅更改“构建”目标,而且更改“启动”目标,在bug(调试)符号和三角形(运行)符号的右侧显示。

相关问题