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/