开发者

How to list all files that include a header file

Standard cscope search for "Find files #including this file"

returns only those matches where foo.h is directly included in bar.c

But I am interested in all those files that directly or indirectly

(e.g开发者_运维知识库. including a header file that includes foo.h) include foo.h


If you're using GCC, run cpp -H on all modules and grep for the header you want:

header=foo.h

# *.c or all interesting modules
for i in *.c; do
    # there's bound to be a cleaner regex for this
    cpp -H "$i" 2>&1 >/dev/null | grep -q "^\.* .*/$header" && echo "$i"
done


This SO post might help you out:

make include directive and dependency generation with -MM

Basically, make can generate a list of all dependancies in a project. I use the following in all my make files:

# Generate dependencies for all files in project
%.d: $(program_SRCS)
    @ $(CC) $(CPPFLAGS) -MM $*.c | sed -e 's@^\(.*\)\.o:@\1.d \1.o:@' > $@

clean_list += ${program_SRCS:.c=.d}

# At the end of the makefile
# Include the list of dependancies generated for each object file
# unless make was called with target clean
ifneq "$(MAKECMDGOALS)" "clean"
-include ${program_SRCS:.c=.d}
endif

An example of what this does. Let's say you have foo.cpp that includes foo.h which includes bar.h which includes baz.h. The above would generate the file foo.d and include it in your make file. The dependancy file foo.d would look like this:

foo.d foo.o: foo.cpp foo.h bar.h baz.h

This way both make and you can see the full build dependancy chain for any particular object file.

Then to find all files which include a particular header just grep -l foo.h *.d to find out which source files include foo.h.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜