build - 如何使用 CMake 收集源文件而不使用 globbing?

CMake documentation明确声明 file(GLOB ...) 不是 建议为构建收集源文件,但它没有 提及推荐的方法实际上是什么。

手动指定每个源文件听起来有点过于手动 大部头书。那么,收集源文件的正确方法是什么,如果不是 文件(GLOB ...)?

最佳答案

手动确实是推荐的方法。通过建议不要使用 GLOB,该文档只是对依赖于现有文件的构建系统发出警告。例如,您想添加一个测试可执行文件,因此您创建了 mytest.cpp。哎呀。现在你的库编译中断了。 AUX_SOURCE_DIRECTORY 的文档(类似于源文件的通配符)给出了以下警告:

It is tempting to use this command to avoid writing the list of source files for a library or executable target. While this seems to work, there is no way for CMake to generate a build system that knows when a new source file has been added. Normally the generated build system knows when it needs to rerun CMake because the CMakeLists.txt file is modified to add a new source. When the source is just added to the directory without modifying this file, one would have to manually rerun CMake to generate a build system incorporating the new file.

如果您确定需要目录的所有内容,并且不打算添加新内容,那么请务必使用 GLOB。

另外,不要忘记手动列出文件不必输入所有文件名。例如,您可以执行 ls *.cpp >> CMakeLists.txt,然后使用编辑器将文件列表移动到文件中的正确位置。

https://stackoverflow.com/questions/10914607/

相关文章:

xcode - 了解 Xcode 的 Copy Headers 阶段

command-line - 如何从命令行构建 IntelliJ 项目?

ios - 如何在 Xcode 4 中自动增加捆绑版本?

c++ - CMake ExternalProject_Add() 和 FindPackage()

c++ - 如何告诉 CMake 在 Windows 上使用 Clang?

gradle - 如何更改 gradle 插件存储库?

plugins - 未找到自定义 Gradle 插件 ID

visual-studio-2010 - Visual Studio 2010 中的项目构建顺序?

java - 如何使用 Jenkins 参数化构建?

android - 多项目结构的 Gradle 构建速度非常慢