如何使用 Cmake 构建系统使用目录中的所有 *.c 文件? [英] How to use all *.c files in a directory with the Cmake build system?

查看:33
本文介绍了如何使用 Cmake 构建系统使用目录中的所有 *.c 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一个目录下的所有.c文件,并将它们全部添加到SRC文件中以在cmake中编译.如何在 CMakeList.txt 中执行此操作.

I want to find all .c files under a directory and add them all to SRC files to compile in cmake. How can I do this in CMakeList.txt.

对于我可以创建的常规 makefile

for regular makefiles I can create

SPECIFIED_SRC_FILE  = $(foreach d,$(SPECIFIED_SRC_DIRS),$(wildcard $(addprefix $(d)/*,*.c)))

但我不知道如何在 CMakeList.txt 中做这样的事情.

but I couldn't get how to do something like this in CMakeList.txt.

推荐答案

试试这个:

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.

这篇关于如何使用 Cmake 构建系统使用目录中的所有 *.c 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆