插件安裝:
- 安裝C++ extension for VS Code.
- 安裝CMake Tools extension for VS Code.
- 安裝 Test Explorer UI
- 安裝C++ TestMate
環(huán)境配置
參考官方教程:
CMake Tools for Visual Studio Code documentation
vscode給cmake命令傳遞參數(shù)有兩種方式晾蜘。
- 在vscode工程的
settings.json
添加對應(yīng)的參數(shù) - 配置 cmake-presets 來傳遞參數(shù)
使用settings.json
更多選項參考:Configure CMake Tools settings
示例:
settings.json
{
"cmake.useCMakePresets": "always",
"cmake.sourceDirectory": "/Users/yxibng/temp/cmake-test"
}
使用cmake-presets
關(guān)于 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 官網(wǎng)提供了一個例子:
{
"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 設(shè)置, 參考:
Configure and build with CMake Presets in Visual Studio Code
- 添加設(shè)置,支持
CMakePresets.json
- 配置
CMakePresets.json
, 根據(jù)自己的環(huán)境去配置,更多設(shè)置參考上面的鏈接。示例:box2d-lite - 選擇對應(yīng)的preset甜刻, 愉快構(gòu)建吧绍撞。
最終效果: