1.17. Dependencies in a Makefile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
TARGETS=greeting_en greeting_fr greeting_es

all: $(TARGETS)
CFLAGS := -Wall

$(TARGETS):
	gcc main.c $@.c $(CFLAGS) -o $@

greeting_en: greeting_en.c main.c greeting.h
greeting_fr: greeting_fr.c main.c greeting.h
greeting_es: greeting_es.c main.c greeting.h

clean:
	-@rm $(TARGETS)

run:
	$(foreach target,$(TARGETS), ./$(target);)

To compile that file with GCC, the command would be.

make all

When the above command completes successfully, the compilation output would be:

gcc main.c greeting_en.c -Wall -o greeting_en
gcc main.c greeting_fr.c -Wall -o greeting_fr
gcc main.c greeting_es.c -Wall -o greeting_es

To execute the binary generated from the source file, the command would be as shown below.

make run

If everything went fine, the output would be:

./greeting_en;  ./greeting_fr;  ./greeting_es;
Hello World!
Bonjour le monde!
Hola Mundo!