【发布时间】:2021-10-12 09:22:12
【问题描述】:
我正在寻找一种在多个项目之间共享 CMake 脚本的方法。
在一个名为somelib 的存储库中,我有一个cmake 文件夹,并且我想从其他项目中将脚本包含在其中。在somelib 的CMakeLists.txt 中,我包含了来自cmake 文件夹的许多文件。
我的项目将“somelib”声明为外部依赖项,如下所示:
include(FetchContent)
FetchContent_Declare(
somelib
GIT_REPOSITORY git@git.somewhere.com:somewhere/somelib.git
GIT_TAG origin/master
)
FetchContent_MakeAvailable(somelib)
我不确定这是一个好的做法,所以请随时纠正我,但我愿意包含 somelib 的 CMakeLists.txt,以便我的项目包含 somelib 包含的所有文件。
所以在FetchContent_MakeAvailable 之后我做了:
include(${somelib_SOURCE_DIR}/CMakeLists.txt)
这个包含本身工作得很好,但问题在于somelib 的CMakeLists.txt。实际上,它所包含的内容与其位置无关,从而导致如下错误:
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:26 (include):
include could not find load file:
cmake/Cache.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:29 (include):
include could not find load file:
cmake/Linker.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
CMake Error at _build/_deps/somelib-src/CMakeLists.txt:33 (include):
include could not find load file:
cmake/CompilerWarnings.cmake
Call Stack (most recent call first):
CMakeLists.txt:47 (include)
我确实意识到,除了包含CMakeLists.txt,我可以简单地这样做:
include(${somelib_SOURCE_DIR}/cmake/Cache.cmake)
include(${somelib_SOURCE_DIR}/cmake/Linker.cmake)
include(${somelib_SOURCE_DIR}/cmake/CompilerWarnings.cmake)
但是随着时间的推移,这将变得相当大(而且它会在我的所有项目之间重复),所以我希望有一个单一的入口点,我可以包括一个单一的东西来让我手头的一切CMakeLists。有可能吗?
【问题讨论】:
标签: c++ makefile cmake cmake-modules