Thinking about makefile "Automatically Processing the dependency of header files"

Source: Internet
Author: User

Original article: automatically handles the dependency between header files

This article explains in detail how makefile automatically handles header file dependencies. The MAKEFILE file is as follows:

all: mainmain: main.o stack.o maze.ogcc $^ -o $@clean:-rm main *.o.PHONY: cleansources = main.c stack.c maze.cinclude $(sources:.c=.d)%.d: %.cset -e; rm -f $@; \$(CC) -MM $(CPPFLAGS) $< > $@.$$$$; \sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \rm -f $@.$$$$

The explanation of the original article is clear, and I will not analyze it in detail. What I don't understand is in this sentence:

sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \

Can it be changed:

sed 's,\($*\)\.o[ :]*,\1.o : ,g' < $@.$$$$ > $@; \

I think $ @ is redundant. After careful consideration, I went into the test and found that $ @ is definitely indispensable and I checked my testing program.

My dependencies are as follows:

Main. O: Main. c Foo. h

Now I Add the following sentence to foo. h:

#include “barrrrrr.h”

The barrrrrrr. h file does not exist.

When $ @ is not added to the MAKEFILE file, compile the program and output the result:

make: Nothing to be done for 'all'

This is obviously not as expected. Main. c relies on barrrrr. H through Foo. H, and barrrrrr. h does not exist. This should be true only when an error is reported.

View the main. d file:

main.o: main.c foo.h

The original main. d file simply adds the dependency on barrrrr. H!

I found the counterexample and looked back at the role of $ @, so I can understand it very well.

Main. d also needs to have the same dependency as main. O.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.