User Tools

Site Tools


projtec:make

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
projtec:make [2016/09/20 07:50] orelprojtec: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, le Makefile est une solution élégante.// //Pour compiler automatiquement et efficacement un projet de programmation, le Makefile est une solution élégante.//
Line 23: Line 23:
 CC=gcc                      # compilateur CC=gcc                      # compilateur
 CFLAGS=-Wall -g -std=c99    # options de compilation CFLAGS=-Wall -g -std=c99    # options de compilation
-LDFLAGS=-lm                 # options de link+LDFLAGS=                    # options de link 
 +LDLIBS=-lm                  # bibliothèques
 CPPFLAGS=                   # options de preprocessing CPPFLAGS=                   # options de preprocessing
  
Line 31: Line 32:
 # règle spécifique pour générer l'exécutable # règle spécifique pour générer l'exécutable
 toto: toto.o tutu.o tata.o toto: toto.o tutu.o tata.o
- $(CC) $^ -o $@ $(LDFLAGS)+        $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)
  
 # règle générique de compilation des fichiers C (implicite) # règle générique de compilation des fichiers C (implicite)
Line 73: Line 74:
 CC=gcc                      # compilateur CC=gcc                      # compilateur
 CFLAGS=-Wall -g -std=c99    # options de compilation CFLAGS=-Wall -g -std=c99    # options de compilation
-LDFLAGS=-lm                 # options de link+LDFLAGS=                    # options de link 
 +LDLIBS=-lm                  # bibliothèques 
 CPPFLAGS=                   # options de preprocessing CPPFLAGS=                   # options de preprocessing
  
Line 81: Line 83:
 # règle spécifique pour générer l'exécutable # règle spécifique pour générer l'exécutable
 toto: toto.o tutu.o tata.o toto: toto.o tutu.o tata.o
- $(CC) $^ -o $@ $(LDFLAGS)+        $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)
  
 # dépendances explicites # dépendances explicites
Line 92: Line 94:
  rm -f *.o *~ toto  rm -f *.o *~ toto
 </code> </code>
 +
 +Plus d'info sur les régles implicites : https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html#Catalogue-of-Rules
 +
 +==== Pour aller un peu plus loin ====
  
 L'écriture des dépendances peut être fastidieux dans un gros projet, on peut alors utiliser la commande "gcc -MM" L'écriture des dépendances peut être fastidieux dans un gros projet, on peut alors utiliser la commande "gcc -MM"
Line 108: Line 114:
 CC=gcc                      # compilateur CC=gcc                      # compilateur
 CFLAGS=-Wall -g -std=c99    # options de compilation CFLAGS=-Wall -g -std=c99    # options de compilation
-LDFLAGS=-lm                 # options de link+LDFLAGS=                    # options de link 
 +LDLIBS=-lm                  # bibliothèques
 CPPFLAGS=                   # options de preprocessing CPPFLAGS=                   # options de preprocessing
  
Line 116: Line 123:
 # règle spécifique pour générer l'exécutable # règle spécifique pour générer l'exécutable
 toto: toto.o tutu.o tata.o toto: toto.o tutu.o tata.o
- $(CC) $^ -o $@ $(LDFLAGS)+        $(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS)
  
 .PHONY: clean dep .PHONY: clean dep
Line 130: Line 137:
 -include depends.txt -include depends.txt
 </code> </code>
 +
 +Dans ce cas, il faut commencer par faire "make dep" pour générer le fichier des dépendances, puis après on peut faire "make". Il faut penser à remettre à jour les dépendances avec "make dep" quand on ajoute de nouveaux fichiers ou que l'on modifie les //includes// !
 +
 +==== Pour aller encore plus loin ====
 +
 +//Parler de Makefile recursif avec "make -C subdir" ; parler des fonctions %%$%%(wildcard *.c) ; etc.//
 +
 +
 +<code bash Makefile>
 +SOURCES  := $(wildcard *.c)
 +INCLUDES := $(wildcard *.h)
 +OBJECTS  := $(SOURCES:.c=.o)
 +</code>
 +
 +
 +==== Nota Bene ====
 +
 +//Quelques astuces...//
 +
 +  * @ suppresses the normal 'echo' of the command that is executed.
 +  * - 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 'execute this command under make -n' (or 'make -t' or 'make -q') when commands are not normally executed. See also the POSIX specification for make and also §9.3 of the GNU Make manual.
 +
 +Plus d'info : https://stackoverflow.com/questions/3477292/what-do-and-do-as-prefixes-to-recipe-lines-in-make
 +
  
projtec/make.1474357857.txt.gz · Last modified: 2024/03/18 15:05 (external edit)