且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Cmake构建系统中使用目录中的所有* .c文件?

更新时间:2023-09-18 08:50:34

尝试一下:

AUX_SOURCE_DIRECTORY

查找目录中的所有源文件.

Find all source files in a directory.

AUX_SOURCE_DIRECTORY(dir VARIABLE) 

收集指定目录中所有源文件的名称 并将列表存储在提供的变量中.该命令旨在 由使用显式模板实例化的项目使用. 模板实例化文件可以存储在模板"中 子目录并使用此命令自动收集以避免 手动列出所有实例.

Collects the names of all the source files in the specified directory and stores the list in the variable provided. This command is intended to be used by projects that use explicit template instantiation. Template instantiation files can be stored in a "Templates" subdirectory and collected automatically using this command to avoid manually listing all instantiations.

使用此命令来避免写源列表很诱人 库或可执行目标的文件.虽然这似乎可行, CMake无法生成知道何时 新的源文件已添加.通常生成的构建系统 知道何时需要重新运行CMake,因为CMakeLists.txt文件是 修改以添加新的来源.当源刚刚添加到 目录而不修改此文件,则必须手动进行 重新运行CMake以生成包含新文件的构建系统.

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.