如何正确使用 cmake 和 vcpkg link mingw 库?

how to properly link mingw libraries using cmake and vcpkg?

我已经习惯了像这样简单的 cmake 代码

cmake_minimum_required(VERSION 3.20)
project(trial VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 20)

find_package(fmt CONFIG REQUIRED)

add_executable(trial main.cpp)

target_link_libraries(trial PRIVATE fmt::fmt)

然而,它在Mingw上不起作用,错误是这样的

[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\trial.dir/objects.a(main.cpp.obj): in function `void fmt::v8::print<std::vector<int, std::allocator<int> >&>(fmt::v8::basic_format_string<char, fmt::v8::type_identity<std::vector<int, std::allocator<int> >&>::type>, std::vector<int, std::allocator<int> >&)':
[build] C:/PROGRA~1/Vcpkg/INSTAL~1/X64-WI~1/include/fmt/core.h:3208: undefined reference to `__imp__ZN3fmt2v86vprintENS0_17basic_string_viewIcEENS0_17basic_format_argsINS0_20basic_format_contextINS0_8appenderEcEEEE'

所以我必须指定 .a.dll.a 库。

cmake_minimum_required(VERSION 3.20)
project(trial VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 20)

# find_package(fmt CONFIG REQUIRED)
include_directories("C:/Program Files/Vcpkg/installed/x64-mingw-static/include")

add_executable(trial main.cpp)

target_link_libraries(trial "C:/Program Files/Vcpkg/installed/x64-mingw-static/lib/libfmt.a")
# target_link_libraries(trial PRIVATE fmt::fmt)
# target_link_libraries(trial fmt::fmt-header-only)

这有点奇怪,我应该如何正确使用 cmake 和 vcpkg link mingw 库?

Makefile and Makefile2

让我们使用 link_directories:https://cmake.org/cmake/help/latest/command/link_directories.html

include_directories("C:/Program Files/Vcpkg/installed/x64-mingw-static/include")
add_executable(trial main.cpp)
target_link_directories(trial PUBLIC "C:/Program Files/Vcpkg/installed/x64-mingw-static/lib/")
target_link_libraries(trial PRIVATE fmt)

我来晚了,但在这里以防万一:

不需要手动指定库位置,问题出在其他地方。

这个错误是在构建时出现的,所以它似乎是通过 cmake 配置而没有错误的。你能 post cmake conf 日志吗?也许如果您尝试同时指定 --triplet=--host-triplet= ?