Reputation:
I'm trying to improve my understanding how make
runs commands.
I have written this makefile:
TARGET=fmake
TARGET2=test_second
fmake: $(TARGET2).c foo.c\
$(TARGET).c test.h
$(CC) -o $(TARGET) $(TARGET).c foo.c
$(CC) -o $(TARGET2) $(TARGET2).c
foo.c:
echo This is foo.c
clean:
rm -f fmake test_second
CC=$(VAR2)
VAR2=gcc
And when running make
, these shell commands displayed:
gcc -o fmake fmake.c foo.c
gcc -o test_second test_second.c
But I expect that there are three commands displayed (also target foo.c
):
This is foo.c
gcc -o fmake fmake.c foo.c
gcc -o test_second test_second.c
Please, explain why this is so.
Upvotes: 1
Views: 3923
Reputation: 206659
In your Makefile, foo.c
has no dependencies. As long as that file exists, the corresponding actions will not be run.
It's the same thing that happens if you rerun make
. The first thing it will try to build is fmake
. But since all that target's dependencies already exist, and are older than the fmake
file, nothing will be built. Same thing for foo.c
, except that it has no dependencies (so it will never run the actions if the file exists).
Upvotes: 4