自动发现 C 依赖项 [英] Automatically discovering C dependencies

查看:36
本文介绍了自动发现 C 依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我当前的项目编写文档,列出所有 .c 文件,并且每个项目都列出该文件直接或间接包含的每个 .h 文件.

I'm required to write documentation for my current project that lists all .c files and for each one lists every .h file which is directly or indirectly included by that file.

这是一个大型项目,虽然我们有 Makefiles 理论上有这些信息,但这些 Makefiles 有时是不正确的(我们从另一家公司继承了这个项目).我们经常不得不做一个 make clean ;make 让我们的更改真正反映在重新编译中,所以我不想依赖这些 Makefile.

This is a large project, and although we have Makefiles which theoretically have this information, those Makefiles are sometimes incorrect (we inherited this project from another company). We've often had to do a make clean ; make for our changes to actually be reflected in the recompilation, so I don't want to rely on these Makefiles.

那么有没有一种工具可以让我们给它一个 .c 文件的名称和一个包含路径,并让它告诉我们所有直接或间接包含在 .c 文件中的 .h 文件?我们没有任何奇怪的东西像

So is there a tool which lets us give it the name of a .c file and an include path and have it tell us all of the .h files which are directly or indirectly included by the .c file? We don't have anything weird like

#define my_include "some_file.h"
#include my_include

因此该工具不需要完美无缺.任何在包含路径中搜索 .c 和 .h 文件以获取常规包含的内容就足够了.

so the tool doesn't need to be perfect. Anything that searched .c and .h files in an include path for regular includes would be good enough.

推荐答案

我在 Makefile 中所做的是

What I do in my Makefile is

SRCS=$(wildcard *.c)

depend: $(SRCS)
    gcc -M $(CFLAGS) $(SRCS) >depend

include depend

这意味着如果任何源文件被更新,depend 规则将运行,并使用 gcc -M 来更新名为depend 的文件.然后将其包含在 makefile 中,为所有源文件提供依赖规则.

This means that if any of the source files are updated, the depend rule will run, and use gcc -M to update the file called depend. This is then included in the makefile to provide the dependency rules for all the source files.

Make 将在包含文件之前检查文件是否是最新的,因此,只要您运行 make,此依赖规则就会在必要时运行,而无需执行make depend".

Make will check that a file is up to date before including it, so this depend rule will run if necessary whenever you run make without you needing to do a "make depend".

这将在任何文件发生更改时运行.我从来没有发现这是一个问题,但是如果目录中有大量文件,您可能会发现花费的时间太长,在这种情况下,您可以尝试为每个源文件设置一个依赖文件,如下所示:

This will run any time any file has changed. I've never found this a problem, but if you had a huge number of files in the directory you might find it took too long, in which case you could try having one dependency file per source file, like this:

SRCS=$(wildcard *.c)
DEPS=$(SRCS:.c=.dep)

%.dep : %.c
    gcc -M $(CFLAGS) $< >$@

include $(DEPS)

请注意,您可以使用 -MM 代替 -M 来不包含系统标头.

Note that you can use -MM instead of -M to not include system headers.

这篇关于自动发现 C 依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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