How to view all files containing a header file

Standard cscope search for "Search for #include files for this file"

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

But I'm interested in all those files that are directly or indirectly

(e.g. including a header file that includes foo.h) include foo.h

+3
source share
2 answers

If you use GCC, run cpp -Hfor all modules and grepfor the desired header:

header=foo.h

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

This SO post can help you:

make enable directive and create dependencies with -MM

Basically, make can generate a list of all the dependencies in a project. I use the following in all 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

, . , foo.cpp, foo.h, bar.h, baz.h. foo.d make. foo.d :

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

, make .

, , , grep -l foo.h *.d, , foo.h.

0

All Articles