如何通过 Cmake 构建系统使用目录中的所有 *.c 文件?
我想找到一个目录下的所有.c文件并将它们全部添加到src文件中以在cmake中编译。我如何在 CMakeList.txt 中执行此操作。
对于常规 makefile,我可以创建
SPECIFIED_SRC_FILE = $(foreach d,$(SPECIFIED_SRC_DIRS),$(wildcard $(addprefix $(d)/*,*.c)))
,但我不知道如何在 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.
for regular makefiles I can create
SPECIFIED_SRC_FILE = $(foreach d,$(SPECIFIED_SRC_DIRS),$(wildcard $(addprefix $(d)/*,*.c)))
but I couldn't get how to do something like this in CMakeList.txt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
那个古老的通配符怎么样?
How about the good old globbing?
试试这个:
Try this:
GLOB_RECURSE
递归示例它基本上使
*
也进入子目录:然后我们的源代码可以定位为:
和
main .c
使用#include "d/ah"
,ac
使用#include "ah"
。在 CMake 顶层使用
GLOB_RECURSE
(即"*.c"
而不是"src/*.c"
)可能是一个坏主意,因为它可以拾取由 CMake 本身生成的位于build/
下的.c
文件。GitHub 上的可运行示例。
GLOB_RECURSE
recursive exampleIt basically makes the
*
also go into subdirectories:And then our sources could be located for example as:
And
main.c
uses#include "d/a.h"
anda.c
uses#include "a.h"
.Using
GLOB_RECURSE
on the CMake toplevel (i.e."*.c"
instead of"src/*.c"
) is likely a bad idea, as it can pick up.c
files generated by CMake itself which are placed underbuild/
.Runnable example on GitHub.
唯一正确答案是不要这样做。 多个对此进行了详细说明答案在这里。我将在这里总结一下,按重要性降序排列。
aux_source_directory
函数完全不正确,因为它无法检测文件系统中的更改。出于同样的原因,在没有CONFIGURE_DEPENDS
的情况下使用file(GLOB
或file(GLOB_RECURSE
) 是完全不正确的。CONFIGURE_DEPENDS
不是保证有效(见第 1 点)。相反,明确列出您的来源。
1 以下是 CMake 开发人员对此的评价:
The only correct answer is to not do this. This has been detailed in multiple answers here on SO. I will summarize here, in decreasing order of importance.
aux_source_directory
function is outright incorrect since it cannot detect changes in the filesystem. For the same reason, usingfile(GLOB
orfile(GLOB_RECURSE
withoutCONFIGURE_DEPENDS
is outright incorrect.CONFIGURE_DEPENDS
is not guaranteed to work. (See point 1)Explicitly list your sources instead.
1 Here's what the CMake developers have to say about it:
是的,你有两个选择。让我们假设您的文件夹结构与此类似。
如果您要使用
AUX_SOURCE_DIRECTORY
,则必须为每个子目录添加 CMakeLists.txt。然后您必须包含并链接所有这些子目录。这是一项相当艰巨的任务。因此,您可以通过 GLOB 轻松完成工作。就是这样完成的。如果你想使用上面的源代码创建一个库,这是命令:
如果你想使用上面的源代码创建一个可执行文件,这是命令:
Yes, you have two options. Let's assume you the folder structure something similar to this.
If you are to use
AUX_SOURCE_DIRECTORY
you have to add CMakeLists.txt each one of sub directories. Then you have to include and link all those subdirectories. This is quite a difficult task. So you can you GLOB and do the job very easily. This is how it is done.If you want to create a library using above source code this is the command:
If you want to create an executable file using above source code this is the command:
您可以按照 @whitequark 的描述使用 AUX_SOURCE_DIRECTORY ,但它不会真正按照您的预期工作,因为 CMake 将无法确定何时添加新文件(这就是使用通配符)。
You can use
AUX_SOURCE_DIRECTORY
as @whitequark described, but it won't really work as you expect, as CMake will be unable to determine when new files are added (which is kind of the whole point with using a wildcard).