美文网首页
配置vscode支持cmake gtest 细粒度单元测试控制

配置vscode支持cmake gtest 细粒度单元测试控制

作者: yxibng | 来源:发表于2022-10-09 17:35 被阅读0次

插件安装:

  1. 安装C++ extension for VS Code.
  2. 安装CMake Tools extension for VS Code.
  3. 安装 Test Explorer UI
  4. 安装C++ TestMate

环境配置

参考官方教程:
CMake Tools for Visual Studio Code documentation

vscode给cmake命令传递参数有两种方式。

  • 在vscode工程的settings.json添加对应的参数
  • 配置 cmake-presets 来传递参数

使用settings.json

更多选项参考:Configure CMake Tools settings

image.png
image.png

示例:

settings.json

{
    "cmake.useCMakePresets": "always",
    "cmake.sourceDirectory": "/Users/yxibng/temp/cmake-test"
}

使用cmake-presets

关于 cmake-presets

One problem that CMake users often face is sharing settings with other people for common ways to configure a project. This may be done to support CI builds, or for users who frequently use the same build. CMake supports two main files, CMakePresets.json and CMakeUserPresets.json, that allow users to specify common configure options and share them with others. CMake also supports files included with the include field.

提供通用方式来共享工程配置。
一般会有一个base preset, 然后 config preset, build preset, test preset 会继承自base preset。
cmake 官网提供了一个例子:

{
  "version": 5,
  "cmakeMinimumRequired": {
    "major": 3,
    "minor": 23,
    "patch": 0
  },
  "include": [
    "otherThings.json",
    "moreThings.json"
  ],
  "configurePresets": [
    {
      "name": "default",
      "displayName": "Default Config",
      "description": "Default build using Ninja generator",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build/default",
      "cacheVariables": {
        "FIRST_CACHE_VARIABLE": {
          "type": "BOOL",
          "value": "OFF"
        },
        "SECOND_CACHE_VARIABLE": "ON"
      },
      "environment": {
        "MY_ENVIRONMENT_VARIABLE": "Test",
        "PATH": "$env{HOME}/ninja/bin:$penv{PATH}"
      },
      "vendor": {
        "example.com/ExampleIDE/1.0": {
          "autoFormat": true
        }
      }
    },
    {
      "name": "ninja-multi",
      "inherits": "default",
      "displayName": "Ninja Multi-Config",
      "description": "Default build using Ninja Multi-Config generator",
      "generator": "Ninja Multi-Config"
    },
    {
      "name": "windows-only",
      "inherits": "default",
      "displayName": "Windows-only configuration",
      "description": "This build is only available on Windows",
      "condition": {
        "type": "equals",
        "lhs": "${hostSystemName}",
        "rhs": "Windows"
      }
    }
  ],
  "buildPresets": [
    {
      "name": "default",
      "configurePreset": "default"
    }
  ],
  "testPresets": [
    {
      "name": "default",
      "configurePreset": "default",
      "output": {"outputOnFailure": true},
      "execution": {"noTestsAction": "error", "stopOnFailure": true}
    }
  ],
  "vendor": {
    "example.com/ExampleIDE/1.0": {
      "autoFormat": false
    }
  }
}

vscode cmake presets 设置, 参考:
Configure and build with CMake Presets in Visual Studio Code

  1. 添加设置,支持CMakePresets.json
    image.png
  2. 配置CMakePresets.json, 根据自己的环境去配置,更多设置参考上面的链接。示例:box2d-lite
  3. 选择对应的preset, 愉快构建吧。

最终效果:


image.png

相关文章

网友评论

      本文标题:配置vscode支持cmake gtest 细粒度单元测试控制

      本文链接:https://www.haomeiwen.com/subject/lzvgartx.html