CMake 添加 fmt 库
CMake add fmt library
我有一个 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.
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 和 [=21] 上工作=] 原样(当然除非您在实施中使用 Linux-specific headers)。
我有一个 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.
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 和 [=21] 上工作=] 原样(当然除非您在实施中使用 Linux-specific headers)。