Search code examples
c++cmakevcpkg

CMake undefined reference when using fmt from vcpkg


I'm pretty new at VSCode CMake and vcpkg, but I get in trouble when setting up environment.

Here's 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)

Here's my example for fmt:

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

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

Here's error CMake gave:

[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

But building with FetchContent works fine, what happened ?


Solution

  • The main reason of this is that I should use "fmt-header-only" instead of "fmt"