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

查看:1417
本文介绍了如何在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天全站免登陆