【问题标题】:VSCode, C++ and CMake - One launch configuration per targetVSCode、C++ 和 CMake - 每个目标一个启动配置
【发布时间】:2021-07-14 17:08:09
【问题描述】:

我使用 Visual Studio Code 和 CMake extension 来处理一个大项目。该项目由一些库和可执行文件组成,它们都可以作为单独的 cmake 构建目标使用。

我将 VSCode / tasks.json 配置为使用其默认 build.task 构建选定的目标:

{
  "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 调试器都会使用选定的启动目标启动。

这很好用,除了只有 一个 的启动目标需要我提供的 "args": ["--serialPort","/dev/ttyS0"]。其他一些人拒绝从给定的这个参数开始。

那么,有没有办法在 VSCode 中使用 cmake 为单独的目标设置单独的参数?

【问题讨论】:

    标签: c++ visual-studio-code cmake


    【解决方案1】:

    您可以使用扩展名Command Variable 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
      
    2. 打开 VSC 的开发者工具:帮助 > 切换开发者工具

    3. 清除控制台

    4. 使用 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
    

    【讨论】:

    • 非常感谢您指出解决问题的有希望的方法!很高兴发现对我来说新的东西:-) 但不幸的是它没有在我的配置上运行。 VSCode 显示错误框"Running the contributed command 'extension.commandvariable.file.fileAsKey' failed."。但在 CMake/Build 日志(日志级别 = 跟踪)中,它显示 extension] [1647] cmake.launchTargetPath finished (returned ".../proj1"properly。你有什么建议我可以找出它失败的原因吗?
    • @phibel 查看我的编辑。让我知道记录了哪些步骤。
    • 所以,我做到了,奇怪的是,日志中只有 [Extension Host] [CMakeTools] 项目可见,而 [Extension Host] commandvariable.file.fileAsKey 一个也没有。
    • 你确定你已经安装了扩展并重启了VSC
    • 重启VSCode后有4个相关项:1)[Extension Host] commandvariable.file.fileAsKey: debug logs enabled,2)[Extension Host] commandvariable.file.fileAsKey: use command variable: ${command:cmake.launchTargetPath},3)[Extension Host] commandvariable.file.fileAsKey: execute command: cmake.launchTargetPath,4)[Extension Host] commandvariable.file.fileAsKey: execute command result: _path_trimmed_/proj1,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多