安装
vscode下载地址
cmake下载地址
vcpkg下载地址
创建CMake项目
// main.cpp
#include <fmt/core.h>
int main()
{
fmt::print("Hello World!\n");
return 0;
}
// CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
find_package(fmt CONFIG REQUIRED)
add_executable(HelloWorld main.cpp)
target_link_libraries(HelloWorld PRIVATE fmt::fmt)
// CMakePresets.json
{
"version": 2,
"configurePresets": [
{
"name": "vcpkg",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "[你的vcpkg安装路径]/scripts/buildsystems/vcpkg.cmake"
}
}
]
}
// CMakeUserPresets.json
{
"version": 2,
"configurePresets": [
{
"name": "default",
"inherits": "vcpkg",
"environment": {
"VCPKG_ROOT": "[你的vcpkg安装路径]"
}
}
]
}
(vcpkg自行添加环境变量)打开命令行,输入命令创建
vcpkg new --application
添加fmt库测试
vcpkg add port fmt
添加之后的vcpkg.json
{
"dependencies": [
"fmt"
]
}
配置vscode
ctrl+p打开命令窗口
输入settings.json
在json文件中加入cmake插件的配置
{
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE" : "F:/sdk/vcpkg/scripts/buildsystems/vcpkg.cmake",
},
}
编译测试大功告成
最后附上官方文档链接