CMake 添加 fmt 库

时间:2021-03-08 14:01:11

标签: c++ cmake fmt

我有一个 CMake 项目,结构如下:

cmake_minimum_required(VERSION 3.11)
project(ReadGraph)

# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
  message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()

# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
                 ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
                 EXCLUDE_FROM_ALL)

SET(CMAKE_CXX_FLAGS "-std=c++20 -pthread -O3")
include_directories(include)

enable_testing()
file(GLOB TESTS "test/*.cpp" "test/*/*.cpp" "src/*.cpp" "src/*/*.cpp")
add_executable(Tests ${TESTS})
target_link_libraries(Tests gtest_main)

还有这个 CMakeLists.txt.in 文件:

cmake_minimum_required(VERSION 3.11)

project(googletest-download NONE)

include(ExternalProject)
ExternalProject_Add(googletest
  GIT_REPOSITORY    https://github.com/google/googletest.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)

我从 GoogleTest 说明中复制粘贴了其中的大部分内容。

使用 Gtests 非常适合我。

我想我想在我的项目中使用 FMT Library

我尝试了一些方法(例如从 gtest 中直接复制方法)将 FMT 包含到我的项目中,但失败了。

我想让 CMake 下载这个库并将其嵌入到我的项目中,这样我就可以使用 FMT 如下:

#include <fmt/core.h>

目前我收到以下错误消息:

fatal error: fmt/core.h: No such file or directory
   10 | #include <fmt/core.h>
      |          ^~~~~~~~~~~~
compilation terminated.

1 个答案:

答案 0 :(得分:4)

CMake 3.11 引入了 FetchContent 模块正是为了这个目的:在配置时下载 3rdparty 依赖项并将它们构建为项目本身的一部分。所以为了同时使用 GTest 和 FMT,你可以使用

# While FetchContent only requires CMake 3.11, selecting C++20 through
# CMAKE_CXX_STANDARD requires 3.12 and the convenience function
# `FetchContent_MakeAvailable` was introduced in CMake 3.14
cmake_minimum_required(VERSION 3.14) 
project(ReadGraph)

include(FetchContent)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

FetchContent_Declare(googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG master
)
FetchContent_MakeAvailable(googletest)

FetchContent_Declare(fmt
  GIT_REPOSITORY https://github.com/fmtlib/fmt.git
  GIT_TAG master
)
FetchContent_MakeAvailable(fmt)

find_package(Threads REQUIRED) # for pthread

include_directories(include)

enable_testing()
file(GLOB TESTS "test/*.cpp" "test/*/*.cpp" "src/*.cpp" "src/*/*.cpp")
add_executable(Tests ${TESTS})
target_link_libraries(Tests 
  PRIVATE 
    gtest_main 
    fmt::fmt 
    Threads::Threads
)

我还冒昧地使用 Threads CMake 模块来支持线程并通过 CMake 设置 C++ 标准,这样这个项目就可以在 Windows 和 Linux 上按原样运行(除非您当然使用实现中特定于 Linux 的标头)。