0

我有一个库的 CMakeLists.txt 文件。这是非常基本的:

set(LIB_FILES source/first.cpp)

add_library(first ${LIB_FILES})

我将这些文件放在一个列表中,因为我最终会向库中添加更多源文件。问题是所有文件都在source目录中。而且我不想经常重复这一点。

我也不想使用GLOB模式匹配解决方案,因为我想在添加新文件时必须编辑 CMakeLists.txt 文件。这样,我的构建将重新构建构建解决方案,并且新文件将正确显示(据我了解。我还是 CMake 的新手)。

我尝试将 CMakeLists.txt 文件添加到source目录本身,只是为了构建LIB_FILES列表。那效果不是很好。CMake 中的变量是文件范围的。即使我打破了范围界定(使用PARENT_SCOPE),我仍然必须在每个文件前面加上目录。所以什么也没得到。

我不想将实际的库定义放在source目录中,因为这会在目录中生成所有构建文件source。我不希望那样。此外,我将需要包含不在目录中或source目录下的标题。

我的目录结构如下所示:

libroot (where the project build files should go)
\-source (where the source code is)
\-include (where the headers that the user of the library includes go)

那么如何告诉 CMake 所有源文件都来自该source目录,这样我就不必不断地拼写出来呢?

4

2 回答 2

3

您也可以将add_library调用移至 source/CMakeLists.txt:

set(LIB_FILES first.cpp)
add_library(first ${LIB_FILES})

然后只需add_subdirectory在您的顶级 CMakeLists.txt 中使用:

add_subdirectory(source)
于 2013-07-30T01:23:28.740 回答
2

你可以使用一个简单的宏

macro(AddSrc dst_var basepath_var)
    foreach(file ${ARGN})
        list(APPEND ${dst_var} ${basepath_var}/${file})
    endforeach()
endmacro()

set(MY_SRCFILES "")

AddSrc(MY_SRCFILES path/to/source
    foo.cpp
    bar.cpp
    whatever.cpp
)
于 2013-07-30T01:39:56.357 回答