1

I am trying to understand how makefiles work. This is a sample makefile:

all: prog
x.o: x.cpp globals.hh
    $(CC) -c x.cpp
y.o: y.cpp globals.hh
    $(CC) -c y.cpp
prog: x.o y.o
    $(LD) -o prog x.o y.o -lc

If I do make file and then make some changes in the x.cpp, and then repeat make all which commands are re-run?

From what I understand, the following commands will be re-run:

$(CC) -c x.cpp

and

-o prog x.o y.o -lc

And if I change the headers file instead, I think all three will re-run when I do make all again.

Am I correct? What would be the sequence?

Also, I am trying to understand how a dependency graph of a makefile could look like. I know that all, x.o, y.o and prog are targets here. But does my logic make sense? enter image description here

CRM
  • 4,569
  • 4
  • 27
  • 33
x89
  • 2,798
  • 5
  • 46
  • 110
  • Also see this as it might be relevant: https://stackoverflow.com/a/9160175/708312 – CRM May 02 '20 at 15:07
  • Did you read some [documention about `make`](https://www.gnu.org/software/make/manual/html_node/index.html) and about [the GCC compiler](http://gcc.gnu.org/) or your C++ compiler ? – Basile Starynkevitch May 02 '20 at 15:09

1 Answers1

0

None will be re-run.

When creating x.o, y.o, and prog, each of those will be newer than their respective input files.

make does not remake files that are newer than their inputs, so no commands will be re-run.

From the POSIX spec on make:

EXTENDED DESCRIPTION

The make utility attempts to perform the actions required to ensure that the specified targets are up-to-date. A target shall be considered up-to-date if it exists and is newer than all of its dependencies, or if it has already been made up-to-date by the current invocation of make (regardless of the target's existence or age). A target may also be considered up-to-date if it exists, is the same age as one or more of its prerequisites, and is newer than the remaining prerequisites (if any). The make utility shall treat all prerequisites as targets themselves and recursively ensure that they are up-to-date, processing them in the order in which they appear in the rule. The make utility shall use the modification times of files to determine whether the corresponding targets are out-of-date.

To ensure that a target is up-to-date, make shall ensure that all of the prerequisites of a target are up-to-date, then check to see if the target itself is up-to-date. If the target is not up-to-date, the target shall be made up-to-date by executing the rule's commands (if any). If the target does not exist after the target has been successfully made up-to-date, the target shall be treated as being newer than any target for which it is a prerequisite.

If a target exists and there is neither a target rule nor an inference rule for the target, the target shall be considered up-to-date. It shall be an error if make attempts to ensure that a target is up-to-date but the target does not exist and there is neither a target rule nor an inference rule for the target.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76