简体   繁体   中英

how to properly link mingw libraries using cmake and vcpkg?

I've getting used to simple cmake codes like this

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)

however, it doesn't work on Mingw, the errors are something like this

[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'

soI have to specify the .a and .dll.a lib.

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)

This is kind of strange, what should I do to properly link mingw libraries using cmake and vcpkg?

Makefile and Makefile2

I am rather late, but here it in case it helps:

Specifying library locations by hand shouldn't be required, the problem is somewhere else.

This error is at build time so it seems that it is passed cmake configuration without errors. Can you post the cmake conf log? perhaps if you tried specifying both --triplet= and --host-triplet= ?

lets use 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM