Differences
This shows you the differences between two versions of the page.
programming:makefiles [2010/03/10 15:45] cyril |
programming:makefiles [2013/09/19 16:41] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Makefiles ====== | ||
- | |||
- | Makefiles are very useful to automate compilation of multi-file projects in c/c++ with Linux. | ||
- | |||
- | ===== Syntax ===== | ||
- | * variables: | ||
- | < | ||
- | < | ||
- | $(< | ||
- | </ | ||
- | |||
- | * targets: | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | |||
- | * template targets: | ||
- | < | ||
- | %.o:%.cpp | ||
- | < | ||
- | </ | ||
- | |||
- | * special variables: | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | |||
- | * **Mini Example** | ||
- | < | ||
- | COMPILO=g++ | ||
- | SRCSCPP = $(wildcard src/*.cpp) | ||
- | OBJS = $(patsubst %.cpp, | ||
- | prog: $(OBJS) | ||
- | $(COMPILO) -o build/$@ $(OBJS) | ||
- | build/ | ||
- | $(COMPILO) -c -o $@ $^ | ||
- | </ | ||
- | |||
- | |||
- | ===== Example ===== | ||
- | |||
- | <code makefile> | ||
- | INCLUDE= \ | ||
- | -I/ | ||
- | -I/ | ||
- | -I. \ | ||
- | `pkg-config --cflags libglade-2.0` | ||
- | |||
- | BIN_PATH=./ | ||
- | |||
- | LIBS= \ | ||
- | `pkg-config --libs libglade-2.0` \ | ||
- | `pkg-config --libs gtk+-2.0` \ | ||
- | -export-dynamic | ||
- | |||
- | LIBS_PATH= \ | ||
- | -L/lib \ | ||
- | -L/usr/lib \ | ||
- | -L/ | ||
- | -L. | ||
- | |||
- | COMPILO=g++ | ||
- | |||
- | FLAGS= -c -g -Wall -ansi -pedantic | ||
- | #FLAGS= -c -ansi -pedantic | ||
- | |||
- | OBJ= \ | ||
- | file1.o \ | ||
- | file2.o | ||
- | |||
- | |||
- | |||
- | |||
- | # cibles : | ||
- | |||
- | monprog: $(OBJ) | ||
- | $(COMPILO) -o $(BIN_PATH)monprog $(OBJ) $(LIBS_PATH) $(LIBS) | ||
- | |||
- | clean : | ||
- | rm -f *.o | ||
- | |||
- | clear : | ||
- | rm -f *.o monprog | ||
- | |||
- | # compilation des sources : | ||
- | |||
- | file1.o : file1.cpp file1.h | ||
- | $(COMPILO) $(FLAGS) $(INCLUDE) -o file1.o file1.cpp | ||
- | |||
- | file2.o : file2.cpp file2.h | ||
- | $(COMPILO) $(FLAGS) $(INCLUDE) -o file2.o file2.cpp | ||
- | </ | ||
- | |||
- | |||
- | ===== pkg-config ===== | ||
- | |||
- | |||
- | When using libraries, pkg-config is very useful : | ||
- | < | ||
- | pkg-config --cflags libxml++-2.6 | ||
- | pkg-config --libs libxml++-2.6 | ||
- | </ | ||
- | |||
- | |||
- | ===== auto-tools ===== | ||