让CMake递归扫描文件夹?

问题描述 投票:35回答:1

如何设置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目录
  • 每次我更改文件夹结构时都必须更改和修改脚本

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

build build-process cmake build-automation build-system
1个回答
42
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.