VSCode CMake Ninja环境搭建
VSCode 插件安装
CMake
安装 Download CMake
将cmake bin目录设置在系统环境变量PATH中
Ninja
安装 Releases · ninja-build/ninja (github.com)
将ninja.exe
放入cmake的bin目录
CMake插件设置
-
windows
平台将生成器改为Ninja
,如果不改为Ninja
,使用msvc
无法导出compile_commands.json
-
勾选
启用导出compile_commands.json
, 否则clangd
无法分析代码
C++ 插件设置
-
禁用C++ 插件的代码提示
Intelli Sense Engine
,使用clangd的代码提示
Ninja MSVC编译问题
-
Q: 延迟加载DLL,无法解析的外部符号 __delayLoadHelper2
A: 需要链接
delayimp
库,例如target_link_libraries(xxx PRIVATE delayimp)
Ninja 使用VS2022,并且想使用VS2017工具集
使用CMakePresets.json
{
"version": 2,
"configurePresets": [
{
"name": "base",
"displayName": "base",
"description": "",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/install/${presetName}"
},
"architecture": {
"value": "x64",
"strategy": "external"
},
"hidden": true
},
{
"name": "MSVC-v141",
"cacheVariables": {
"CMAKE_CXX_COMPILER": "cl.exe",
"CMAKE_C_COMPILER": "cl.exe"
},
"toolset": {
"strategy": "external",
"value": "v141,host=x64"
},
"hidden": true
},
{
"name": "GCC",
"cacheVariables": {
"CMAKE_CXX_COMPILER": "",
"CMAKE_C_COMPILER": ""
},
"hidden": true
},
{
"name": "Clang",
"cacheVariables": {
"CMAKE_CXX_COMPILER": "clang++",
"CMAKE_C_COMPILER": "clang"
},
"hidden": true
},
{
"name": "Debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
},
"hidden": true
},
{
"name": "Release",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
},
"hidden": true
},
{
"name": "MSVC-v141-Debug",
"inherits": [
"base",
"Debug",
"MSVC-v141"
]
},
{
"name": "MSVC-v141-Release",
"inherits": [
"base",
"Release",
"MSVC-v141"
]
},
{
"name": "GCC-Debug",
"inherits": [
"base",
"Debug",
"GCC"
]
},
{
"name": "GCC-Release",
"inherits": [
"base",
"Release",
"GCC"
]
},
{
"name": "Clang-Debug",
"inherits": [
"base",
"Debug",
"Clang"
]
},
{
"name": "Clang-Release",
"inherits": [
"base",
"Release",
"Clang"
]
}
]
}
通过 toolset
指定 v141
工具集
"toolset": {
"strategy": "external",
"value": "v141,host=x64"
},