Please disable Adblockers and enable JavaScript for domain CEWebS.cs.univie.ac.at! We have NO ADS, but they may interfere with some of our course material.

make

make ist ein nützliches Tool zum Kompilieren von Programmen. Aufgrund seiner flexiblen Architektur kann es aber auch zum Erzeugen von Graphen und anderen Artefakten verwendet werden. 
 
In einem Makefile werde Regeln zum erzeugen von Dateien (targets) definiert. Mit  
make target
wird die gewählte Datei erzeugt. Wird kein target angegeben wird die im Makefile zuerst stehende Regel ausgeführt. 
 
Um das Kompilieren von großen Programmen zu beschleunigen, prüft make zuerst ob die zu erzeugende Datei bereits existiert und noch aktuell ist, nur wenn dies nicht zutrifft wird die Regel ausgeführt. 

Regeln

Ein Makefile definiert Regeln zum Erzeugen von Dateien.  
 
Regeln in einem Makefile haben folgende form: 
target: Abhängigkeiten
<TAB>      Befehl 1
<TAB>     Befehl 2
...
Achtung vor jedem Befehl muss ein <tabulator> Zeichen stehen!  
 
target
Normalerweise der Name der zu generierenden Datei. Es kann aber auch der Name einer auszuführenden Aktion sein.
Abhängigkeiten (optional)
Die abhängigen Dateien bzw. Aktionen um eine Datei zu erzeugen bzw. eine Aktion auszuführen.
Befehle (optional)
Die für die Erzeugung der Datei benötigten Befehle. Z.B. Compiler-Aufruf

Makros (Variablen)

Makros erlauben immer wieder kehrende Bestandteile eines Makefiles einer Variable zu zuordnen. Dies ermöglicht zb. schnell den verwendeten Compiler zu wechseln, usw. 
 

Kommentare

Kommentare beginnen in einem Makefile mit # und enden mit dem Ende der Zeile. 

.PHONY Targets

Beispiel Makefile

# makro definition
ATLASCFLAGS=-I/usr/local/atlas/include
ATLASLDFLAGS=/usr/local/atlas/lib/libcblas.a /usr/local/atlas/lib/libatlas.a -lm
# overwrite default makros
CFLAGS=-m64 -O2 -std=gnu99 $(ATLASCFLAGS) -msse -msse2 -msse3
LDFLAGS=$(ATLASLDFLAGS) -lpapi -lm
CC=gcc
 
TARGET=benchmark
OBJ=benchmark.o my_dgemm.o
 
#default target
all: $(TARGET)
 
# target for test runs
.PHONY: test
test: $(TARGET)
        for n in 31 32 33 63 64 127 128 129 255 256 257 511 512 513 767 768 769 1023 1024 1025 2047 2048;\
            do ./$(TARGET) -n $$n;\
            done
 
# target to compile
$(TARGET): $(OBJ)
        @echo 'Building target: $@'
        $(CC)  -o $@ $(OBJ) $(CFLAGS) $(LDFLAGS)
        @echo 'Finished building target: $@'
 
# make clean
.PHONY: clean
clean:
        -@rm *.o $(TARGET)

Vordefinierte Makros

CC
Program for compiling C programs; default ‘cc’.
CXX
Program for compiling C++ programs; default ‘g++’.
FC
Program for compiling or preprocessing Fortran and Ratfor programs; default ‘f77’.
 
CFLAGS
Extra flags to give to the C compiler.
CXXFLAGS
Extra flags to give to the C++ compiler.
FFLAGS
Extra flags to give to the Fortran compiler.
LDFLAGS
Extra flags to give to compilers when they are supposed to invoke the linker, ‘ld’, such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead.
LDLIBS
Library flags or names given to compilers when they are supposed to invoke the linker, ‘ld’.
Mehr unter «http://www.gnu.org/software/make/manual/make.html#Implicit-Variables» 

Automatische Variablen

$@
The file name of the target of the rule.
$&lt;
The name of the first prerequisite. If the target got its recipe from an implicit rule, this will be the first prerequisite added by the implicit rule.
$?
The names of all the prerequisites that are newer than the target, with spaces between them.
$^
The names of all the prerequisites, with spaces between them.
Mehr unter «http://www.gnu.org/software/make/manual/make.html#Automatic-Variables» 
Letzte Änderung: 04.03.2015, 16:01 | 445 Worte