40

如何设置 CMake 以递归扫描给定目录并确定源文件列表?

我的项目是一个共享库。我有一个类似于这样的文件夹结构:

/
  src/              # Source files in an arbitrary tree
  include/          # Headers, tree mirrors that of the src/ folder
  examples/         # Executable code examples that link against the library
  CMakeLists.txt

我希望 CMake 递归扫描srcinclude确定我的项目中的源文件和头文件列表,而不管目录结构如何。我也想避免:

  • 用无穷无尽的文件污染src/和目录include/CMakeLists.txt
  • 每次更改文件夹结构时都必须更改和调整脚本

但是,每个示例都有自己的构建脚本很好。

4

1 回答 1

43

CMake 为递归文件 globing 提供了以下命令:

file(GLOB_RECURSE variable [RELATIVE path] 
   [FOLLOW_SYMLINKS] [globbing expressions]...)

命令文档:http ://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:file

于 2012-03-16T23:23:03.747 回答