我使用 fmt 库编写了一个 hello world 程序,尝试使用 cmake 将库包含在我的项目中,而无需提前安装库来将项目转移给其他人。但它无法构建。
我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG master
)
FetchContent_MakeAvailable(fmt)
add_executable(HelloWorld helloworld.cpp)
target_link_libraries(HelloWorld PRIVATE fmt::fmt)
set(CMAKE_GENERATOR "MinGW Makefiles" CACHE STRING "CMake generator" FORCE)
set(CMAKE_CXX_COMPILER "C:/msys64/mingw64/bin/g++")
set(CMAKE_C_COMPILER "C:/msys64/mingw64/bin/gcc")
我的helloworld.cpp:
#include <fmt/core.h>
int main()
{
fmt::print("Hello World!\n");
return 0;
}
在终端我写:
cmake -G "MinGW Makefiles" D:\Users\aLL_in\projects\helloworld_2
然后我写道:
cmake --build D:\Users\aLL_in\projects\helloworld_2\build
我该怎么办? PS 我使用 VS Code
您还可以使用ExternalProject_Add 函数来代替获取。无论哪种方式,您都需要安装 fmt 项目,但您没有这样做。另一种选择可能是这样的:
ExternalProject_Add(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG master
PREFIX ${CMAKE_BINARY_DIR}/fmt
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=/usr/local/)
set(FMT_INCLUDE_DIR ${CMAKE_INSTALL_PREFIX}/include/)
set(FMT_LIBRARY_DIR ${CMAKE_INSTALL_PREFIX}/lib/)
set(FMT_LIBRARIES libfmt.a)
我还没有测试过。