cmake add_subdirectory 使用除 CMakeLists.txt 之外的其他 cmake 文件名

cmake add_subdirectory with other cmake filename than CMakeLists.txt

CMake 文档指出,当执行 add_subdirectory 时,cmake 在目录中查找 CMakeLists.txt。有什么办法可以更改makefile 的名称吗?例如,如果我有两个用于两种完全不同配置的 cmake 文件并且不想将所有内容混合在一个文件中。

当然我可以创建 CMakeLists.txt 并根据配置在其中包含其他内容,但我只是好奇是否可以制作 add_subdirectory 寻找任意名称的 make 文件

不,没有办法做到这一点。文件名 CMakeLists.txt 在 CMake 本身中是不可配置的硬编码。正如您所说,您最好的选择是创建一个 "signpost" CMakeLists.txt 文件,该文件将根据您需要的任何逻辑 include() 真实内容。

我知道这个问题很老了,但我还是会回答,以防它对某人有帮助。根据本书 "Professional CMake" 第 3 版,您应该使用

include(文件名[可选] [RESULT_VARIABLE myVar] [NO_POLICY_SCOPE])

"include() expects the name of a file to read in, whereas add_subdirectory() expects a directory and will look for a CMakeLists.txt file within that directory. The file name passed to include() typically has the extension .cmake, but it can be anything." 第 56 页。

需要注意 add_subdirectory() 和 include() 之间的其他差异,因此我建议您获取并阅读这本书。