让CMake递归扫描文件夹?

Mat*_*ira 38 build-automation build-process build cmake build-system

如何设置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
Run Code Online (Sandbox Code Playgroud)

我想避免:

  • 用无尽的文件污染srcinclude目录src/
  • 每次我更改文件夹结构时都必须更改和修改脚本

但是,每个示例都可以拥有自己的构建脚本.

And*_*aev 42

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

file(GLOB_RECURSE variable [RELATIVE path] 
   [FOLLOW_SYMLINKS] [globbing expressions]...)
Run Code Online (Sandbox Code Playgroud)

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

  • 请注意,如果使用此方法,则每次添加或删除源文件时都必须重新生成cmake. (11认同)
  • @mrgrieves有更有效的方法吗?如果是这样,请考虑在此问题上添加另一个答案. (6认同)
  • 这是一个非常糟糕的示例,因为文件(GLOB_RECURSIVE)不允许您有选择地选择子目录。 (2认同)