Hi,
I would like to make some notices about documentation for GNU Make utility in its part
which concerns automatic generation dependency files for C/C++ sources. This is
paragraph "4.13 Generating Prerequisites Automatically".
According to documentation dependency rules for C/C++ files could be generated
with -M compiler flag. But -M flag generates dependency rule, which contains, among
others, system include files in prerequisite list. On Unix system These prerequisites
start usually with " /usr/include/" prefix. Normally system includes are not so important
for application project, and -MM compiler flag is more suitable as it ends up with shorter
dependency list.
1) So the suggestion is to replace "-M" compiler flag to "-MM" one.
My second suggestion for this chapter concerns the recipe of rule for generating
dependency file. The current rule is redundantly complex.
Modern GCC compiler, when it is used for dependency generation, allows to specify output file name with -MF. Also it is possible to specify target name for generated rules with -MT flag. The -MT flag can be used multiple times in single command.
2) The second suggestion is to replace example rule for dependency generation:
Current one is:
%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed ’s,\($*\)\.o[ :]*,\1.o $@ : ,g’ < $@.$$$$ > $@; \
rm -f $@.$$$$
Replacement:
%.d: %.c
$(CC) $(CFLAGS) -MF $@ -MT $(patsubst %.d,%.o,$@) -MT $@ $^
So target file example.d will be updated upon update of example.c with the
recipe working as follows:
-MF $@ output file name will be the name of the rule's target
-MT $(patsubst %.d,%.o,$@) the first target in dependency rule will be the name
of object file
-MT $@ the second target in dependency rule will be the name of dependency file
$^ is a "paranoid check" that there will be definitely alone example.c file used
Having single command for dependency generation will increase speed of compilation.
Also there is no need for temporary files reading/writing anymore, which also makes
performance penalty.
hope my suggestions are useful...
Thanks, Nikoay Vakhlyarskiy.