使用 vcpkg 中的 fmt 时 CMake undefined reference

CMake undefined reference when using fmt from vcpkg

我在 VSCode CMake 和 vcpkg 方面还很新,但是在设置环境时遇到了麻烦。

这里是CMakeList.txt

cmake_minimum_required(VERSION 3.0.0)
project(temp VERSION 0.1.0)

set(CMAKE_TOOLCHAIN_FILE "C:/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(VCPKG_TARGET_TRIPLET "x64-windows")

#include("C:/Users/dell/vcpkg/scripts/buildsystems/vcpkg.cmake")

#include(FetchContent)

#FetchContent_Declare(fmt
#  GIT_REPOSITORY https://github.com/fmtlib/fmt.git
#  GIT_TAG master
#)

#FetchContent_MakeAvailable(fmt)

add_executable(temp C++/temp/temp.cpp)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/C++/temp)

find_package(fmt CONFIG REQUIRED)
target_link_libraries(temp PRIVATE fmt::fmt)

这是我的 fmt 示例:

#include <iostream>
#include "fmt/format.h"

int main() {
    fmt::print("This is a string {}", "world");
}

这是 CMake 给出的错误:

[main] Building folder: VS Code Project 
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build "c:/Users/dell/Desktop/VS Code Project/build" --config Debug --target all -j 14 --
[build] Consolidate compiler generated dependencies of target temp
[build] [ 50%] Building CXX object CMakeFiles/temp.dir/C++/temp/temp.cpp.obj
[build] [100%] Linking CXX executable ..\C++\temp\temp.exe
[build] E:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\temp.dir/objects.a(temp.cpp.obj): in function `void fmt::v8::print<char const (&) [6]>(fmt::v8::basic_format_string<char, fmt::v8::type_identity<char const (&) [6]>::type>, char const (&) [6])':
[build] C:/Users/dell/vcpkg/installed/x64-windows/include/fmt/core.h:3208: undefined reference to `__imp__ZN3fmt2v86vprintENS0_17basic_string_viewIcEENS0_17basic_format_argsINS0_20basic_format_contextINS0_8appenderEcEEEE'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [CMakeFiles\temp.dir\build.make:100: ../C++/temp/temp.exe] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/temp.dir/all] Error 2
[build] mingw32-make: *** [Makefile:110: all] Error 2
[build] Build finished with exit code 2

但是使用 FetchContent 构建工作正常,发生了什么事?

这样做的主要原因是我应该使用“fmt-header-only”而不是“fmt”