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

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

问题描述

我需要写文档,我当前的项目,列出了所有.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.

这是一个大项目,虽然我们有Makefile文件理论上有这样的信息,这些Makefile文件有时是不正确的(我们继承从另一家公司这个项目)。我们常常不得不做了使清洁;让的实际反映在我们重新编译的变化,所以我不希望依靠这些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文件在include路径包括定期就足够了。

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

这意味着,如果任何一个源文件被更新,则取决于规则将运行,并且使用gcc -M更新名为依赖于文件。这是遂把在生成文件,为所有的源文件提供的相关性规则。

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 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.

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

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