Skip to content

Instantly share code, notes, and snippets.

@markhc
Last active July 16, 2019 13:10
Show Gist options
  • Save markhc/1594360f314ab9357cb4e3f9405821b2 to your computer and use it in GitHub Desktop.
Save markhc/1594360f314ab9357cb4e3f9405821b2 to your computer and use it in GitHub Desktop.
fmtlib + cmake
cmake_minimum_required (VERSION 3.12)
# not strictly needed, but it helps when you forget to pass the CMAKE_TOOLCHAIN_FILE as argument to cmake
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE
"$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "")
endif()
project(cmake_example CXX)
# finds the fmt installation
find_package(fmt CONFIG REQUIRED)
add_executable(main main.cpp)
target_compile_features(main PUBLIC cxx_std_17)
# links with fmt. Some magic happens here
target_link_libraries(main fmt::fmt)
# assuming you're in the directory with both of these files
mkdir build
cd build
# replace path below with your own instalattion folder ofc
cmake .. -DCMAKE_TOOLCHAIN_FILE=/home/markhc/Coding/vcpkg/scripts/buildsystems/vcpkg.cmake
#include <fmt/printf.h>
int main() {
fmt::print("{}, {}. {} {}\n", "Hello", "there", 42, 3.1415);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment