How MAKE knows which source file to check

2020-05-07 09:48发布

问题:

How this simple make script knows that some of cpp files is changed? Does it means that for each .o file it will look for corresponding .cpp one? What if extension will be different - for example .c

hellomake: hellomake.o hellofunc.o 
    gcc -o hellomake hellomake.o hellofunc.o -I.

UPD: According to my understanding scrip I provide should not look to c and cpp files. And when I asked to build project second time MAKE told me "make: 'hellomake' is up to date. But I was surprised when I have changed hellomake.cpp MAKE has decided do rebuild project. Why?

回答1:

GNU make has many builtin rules. Run make -p to find them. And use the existing rules in your Makefile, see this or that or this



回答2:

Obvious documentation links were already provided. I just wanted to comment on your example. You told make the following:

The file hellomake relies on hellomake.o and hellofunc.o, ie. both are prerequisites of hellomake. If any prerequisite changes since the last build, hellomake will be rebuilt. How (re-) building is done is the second line, ie. the gcc invocation.

To answer your question: The snippet provided, will not look for any cpp files. You need different rules in addition for that, ie. something like

%.o: %.cpp
    gcc -I. -c @< -o $@ 

In case you are searching for a rather generic Makefile to start with, I'd recommend this one. It has been the basis for many of my Makefiles in use.



标签: c++ gcc makefile