1

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

isudfv
  • 179
  • 1
  • 9
  • What does it do? Did you look at the generated makefile? I use fmt from vcpkg but I am using msvc. Also did you specify the toolchain file? – drescherjm Mar 12 '22 at 03:33
  • I can hardly understand the Makefile, but I 've updated it with Makefile and Makefile2. it works fine on both x64-windows and x64-linux except x64-mingw. toolchain is specified with `"-DCMAKE_TOOLCHAIN_FILE=C:\\Program Files\\Vcpkg\\scripts\\buildsystems\\vcpkg.cmake"` – isudfv Mar 12 '22 at 03:46

2 Answers2

2

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= ?

pattakosn
  • 365
  • 4
  • 13
  • 1
    Thanks for your advice, It reminds me of the `triplet` problem. It turns out that cmake is trying to use fmt in `x64-window`. The problem is solved after setting `-DVCPKG_TARGET_TRIPLET=x64-mingw-static`. It's a pity cmake can't deduce triplet from toolkit – isudfv Apr 24 '22 at 09:58
  • 1
    I do not think any of these is cmake's issue. apparently vcpkg by default prefers `x64-windows` which is "officially supported" whereas the `x??-mingw-*` ones are "community based". – pattakosn Apr 24 '22 at 10:06
0

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)
long.kl
  • 670
  • 4
  • 12
  • kind of weird still, I just want to find_package() once for all, is it feasible? – isudfv Mar 12 '22 at 04:29
  • What does `One for all` mean ? – long.kl Mar 12 '22 at 05:32
  • means I can simply `find_package()` and `target_link_libraries(target fmt::fmt)` , no need to take platform and different lib files into account – isudfv Mar 12 '22 at 07:40
  • I don't know what command can do that . The answer only show you how to link a lib without specify path of each one. – long.kl Mar 12 '22 at 08:32
  • 2
    He is right. He should be able to just use `find_package()` and `target_link_libraries(target fmt::fmt)`. There should be no need to meddle with the link directories. This should be reported upstream to vcpkg or fmt itslef. – stardust Mar 12 '22 at 10:12
  • Its probably some bug in the vcpkg building for the `x64-mingw-static` triplet – drescherjm Mar 12 '22 at 15:46