Vscode + clangd + vcpkg + msvc
CMakefileLists.txt 配置
cmake_minimum_required(VERSION 3.12)
# vcpkg triplet
set(VCPKG_TARGET_TRIPLET x86-windows)
# vcpkg cmake
include(path/vcpkg/scripts/buildsystems/vcpkg.cmake)
# generate compile_command.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(demo01)
find_package(fmt CONFIG REQUIRED)
aux_source_directory(. DIR_SRCS)
add_executable(demo ${DIR_SRCS})
target_link_libraries(demo PRIVATE fmt::fmt)
vcpkg
git clone vcpkg
./bootstrap.bat
vcpkg install fmt:x86-windows
clangd
-
安装clangd插件
-
禁用Cpp.intelliage.optimize
-
下载clangd.exe
-
配置settings.json
{ "clangd.path": "path/clangd.exe", "cmake.configureArgs": [ "-DCMAKE_EXPORT_COMPILE_COMMANDS=1" ], }
-
重点:如果使用 gcc or clang mingw 这里已经完成,如果使用 msvc是无法生成 compile_commands.json。解决方案是通过 nijia作为前端,msvc作为后端
- 安装ninja
- 使用 x86 Native Tools Command Prompt for VS 2017 打开 vscode
- cmake build -G Ninja
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${command:cmake.launchTargetPath}",
"args": [
"utf8",
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "integratedTerminal"
}
]
}