插件安装:
- 安装C++ extension for VS Code.
- 安装CMake Tools extension for VS Code.
- 安装 Test Explorer UI
- 安装C++ TestMate
环境配置
参考官方教程:
CMake Tools for Visual Studio Code documentation
vscode给cmake命令传递参数有两种方式。
- 在vscode工程的
settings.json
添加对应的参数 - 配置 cmake-presets 来传递参数
使用settings.json
更多选项参考:Configure CMake Tools settings
示例:
settings.json
{
"cmake.useCMakePresets": "always",
"cmake.sourceDirectory": "/Users/yxibng/temp/cmake-test"
}
使用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
- 添加设置,支持
CMakePresets.json
- 配置
CMakePresets.json
, 根据自己的环境去配置,更多设置参考上面的链接。示例:box2d-lite - 选择对应的preset, 愉快构建吧。
最终效果: