projtec:make
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
projtec:make [2016/09/19 11:34] – created orel | projtec:make [2024/03/18 15:06] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====Makefile==== | + | ======Makefile====== |
//Pour compiler automatiquement et efficacement un projet de programmation, | //Pour compiler automatiquement et efficacement un projet de programmation, | ||
Line 23: | Line 23: | ||
CC=gcc | CC=gcc | ||
CFLAGS=-Wall -g -std=c99 | CFLAGS=-Wall -g -std=c99 | ||
- | LDFLAGS=-L. -lm # options de link | + | LDFLAGS= |
+ | LDLIBS=-lm | ||
CPPFLAGS= | CPPFLAGS= | ||
Line 31: | Line 32: | ||
# règle spécifique pour générer l' | # règle spécifique pour générer l' | ||
toto: toto.o tutu.o tata.o | toto: toto.o tutu.o tata.o | ||
- | $(CC) $^ -o $@ $(LDFLAGS) | + | |
- | # règle générique de compilation des fichiers C | + | # règle générique de compilation des fichiers C (implicite) |
%.o: %.c | %.o: %.c | ||
$(CC) $(CFLAGS) -c $< -o $@ | $(CC) $(CFLAGS) -c $< -o $@ | ||
Line 50: | Line 51: | ||
* La variable %%$%%< représente la première dépendance. | * La variable %%$%%< représente la première dépendance. | ||
* La cible .PHONY permet d' | * La cible .PHONY permet d' | ||
+ | |||
+ | |||
+ | Par ailleurs, il n'est pas nécessaire d' | ||
Pour fabriquer une cible particulière, | Pour fabriquer une cible particulière, | ||
Line 58: | Line 62: | ||
gcc -Wall -g -std=c99 -c tutu.c -o tutu.o | gcc -Wall -g -std=c99 -c tutu.c -o tutu.o | ||
gcc -Wall -g -std=c99 -c tata.c -o tata.o | gcc -Wall -g -std=c99 -c tata.c -o tata.o | ||
- | gcc toto.o tutu.o tata.o -o toto -L. -lm | + | gcc toto.o tutu.o tata.o -o toto -lm |
$ make clean | $ make clean | ||
Line 64: | Line 68: | ||
</ | </ | ||
- | //Expliquer comment injecter automatiquement | + | L' |
+ | |||
+ | <code bash Makefile> | ||
+ | # variable standard pour la compilation | ||
+ | CC=gcc | ||
+ | CFLAGS=-Wall -g -std=c99 | ||
+ | LDFLAGS= | ||
+ | LDLIBS=-lm | ||
+ | CPPFLAGS= | ||
+ | |||
+ | # cible principale (par défaut) | ||
+ | all: toto | ||
+ | |||
+ | # règle spécifique pour générer l' | ||
+ | toto: toto.o tutu.o tata.o | ||
+ | $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) | ||
+ | |||
+ | # dépendances explicites | ||
+ | toto.o: toto.c tata.h tutu.h | ||
+ | tata.o: tata.c tata.h | ||
+ | tutu.o: tutu.c tutu.h | ||
+ | |||
+ | .PHONY: clean | ||
+ | clean: | ||
+ | rm -f *.o *~ toto | ||
+ | </code> | ||
+ | |||
+ | Plus d'info sur les régles implicites : https:// | ||
+ | |||
+ | ==== Pour aller un peu plus loin ==== | ||
+ | |||
+ | L' | ||
+ | |||
+ | <code bash> | ||
+ | $ gcc -MM *.c | ||
+ | toto.o: toto.c tata.h tutu.h | ||
+ | tata.o: tata.c tata.h | ||
+ | tutu.o: tutu.c tutu.h | ||
+ | </ | ||
+ | |||
+ | Pour aller encore plus loin, on peut inclure les dépendances dans le Makefile, que l'on générère explicitement avec la cible dep. | ||
+ | |||
+ | <code bash Makefile> | ||
+ | # variable standard pour la compilation | ||
+ | CC=gcc | ||
+ | CFLAGS=-Wall -g -std=c99 | ||
+ | LDFLAGS= | ||
+ | LDLIBS=-lm | ||
+ | CPPFLAGS= | ||
+ | |||
+ | # cible principale (par défaut) | ||
+ | all: toto | ||
+ | |||
+ | # règle spécifique pour générer l'exécutable | ||
+ | toto: toto.o tutu.o tata.o | ||
+ | $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) | ||
+ | |||
+ | .PHONY: clean dep | ||
+ | |||
+ | # génération des dépendances | ||
+ | dep: | ||
+ | $(CC) | ||
+ | |||
+ | clean: | ||
+ | rm -f *.o *~ toto | ||
+ | |||
+ | # inclusion des dépendances | ||
+ | -include depends.txt | ||
+ | </ | ||
+ | |||
+ | Dans ce cas, il faut commencer par faire "make dep" pour générer le fichier des dépendances, | ||
+ | |||
+ | ==== Pour aller encore plus loin ==== | ||
+ | |||
+ | // | ||
+ | |||
+ | |||
+ | <code bash Makefile> | ||
+ | SOURCES | ||
+ | INCLUDES := $(wildcard *.h) | ||
+ | OBJECTS | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Nota Bene ==== | ||
+ | |||
+ | //Quelques astuces...// | ||
+ | * @ suppresses the normal ' | ||
+ | * - means ignore the exit status of the command that is executed (normally, a non-zero exit status would stop that part of the build). | ||
+ | * + means ' | ||
+ | Plus d'info : https:// | ||
projtec/make.1474284886.txt.gz · Last modified: 2024/03/18 15:05 (external edit)