我想使用 FetchContent
自动管理我的项目对 Eigen 的依赖关系,这通常是有效的。但是,当使用推荐的 FetchContent_Declare()
和 FetchContent_MakeAvailable()
方法时,对 install
的后续调用还会安装所有 Eigen header 和文档,这是在我的情况下没有必要。
为了避免这种行为,我尝试了此答案中建议的方法:Disable install for FetchContent
FetchConten_Populate()
但是无法填充变量 ${Eigen_SOURCE_DIR}
和 ${Eigen_BIN_DIR}
(文档告诉我应该发生这种情况) ),这样我就无法调用 add_subdirectory()
。
这是一个最小的CMakeLists.txt
:
cmake_minimum_required (VERSION 3.12)
project (FetchContentExample)
include (FetchContent)
FetchContent_Declare(
Eigen
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
GIT_TAG 3.4.0
)
FetchContent_GetProperties(Eigen)
if(NOT Eigen_POPULATED)
FetchContent_Populate(Eigen)
message("SRC; ${Eigen_SOURCE_DIR}") # Apparently empty?
message("BIN: ${Eigen_BINARY_DIR}") # Apparently empty?
add_subdirectory(${Eigen_SOURCE_DIR} ${Eigen_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
add_executable(FetchContentExample
main.cpp
)
target_link_libraries (FetchContentExample
PRIVATE
Eigen3::Eigen
)
install(
TARGETS FetchContentExample
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT Runtime
)
当我使用例如时,相同的设置工作正常
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 5.3.0
)
而不是 Eigen 。 当涉及到 Eigen 时,我具体做错了什么?
最佳答案
FetchContent_Populate()
however fails to fill the variables${Eigen_SOURCE_DIR}
and${Eigen_BINARY_DIR}
(which the documentation told me should happen).
实际上,FetchContent填充变量${eigen_SOURCE_DIR}
和${eigen_BINARY_DIR}
这些名称是根据项目名称的小写变体构建的。这写在 documentation :
FetchContent_Populate() will set three variables in the scope of the caller:
<lowercaseName>_POPULATED
This will always be set to TRUE by the call.
<lowercaseName>_SOURCE_DIR
The location where the populated content can be found upon return.
<lowercaseName>_BINARY_DIR
A directory intended for use as a corresponding build directory.
因此 EXCLUDE_FROM_ALL 包含 Eigen 的正确命令顺序是:
FetchContent_GetProperties(Eigen)
if(NOT eigen_POPULATED)
FetchContent_Populate(Eigen)
add_subdirectory(${eigen_SOURCE_DIR} ${eigen_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
关于cmake - 如何将 FetchContent_Populate 与 Eigen 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72362303/