Deutsch English
Your feedback:
Did you like this page? vote3 Yes
vote2 Partly
vote1 No
Your comment?

Add your email address, if you want to get a response

Your name, if you like

Do not change this:
Feedback
Search

Makefiles

06-08-2005 13.09

Comments

# foo bar
06-08-2005 13.09

Rules, Dependency Lines, Target Sources, Shell Lines

Create TARGET which needs TARGETSOURCE1 TARGETSOURCE2 TARGETSOURCE3 ... by running SHELLLINE1 SHELLLINE2 SHELLLINE3 ...
TARGET: TARGETSOURCE1 TARGETSOURCE2 TARGETSOURCE3 ...
SHELLLINE1
SHELLLINE2
SHELLLINE3
...
If TARGET is no real file you should insert a .PHONY TARGET rule:
.PHONY: TARGET
TARGET: TARGETSOURCE1 TARGETSOURCE2 TARGETSOURCE3 ...
...
It is even possible to jump with a SHELLLINE to another TARGET:
TARGET2:
test -e FOO && $(make) TARGET3
27-06-2006 17.52

Inference Rules

Instead of writing lots of rules like this:
foo.obj: foo.c
$(CC) foo.c -o foo.obj

bar.obj: bar.c
$(CC) bar.c -o bar.obj

....obj: ....c
$(CC) ....c -o ....obj
You may also write:
%.obj: %.c
$(CC) $< -o $@
$@ Current TARGET
$(@F) Filename (without path) of the current TARGETS
$< First Current TARGETSOURCES
$^ All current TARGETSOURCES
$? All current TARGETSOURCES the are out-of-date
20-05-2007 00.10

Macros

VAR1 = foo bar
VAR2 = bar $(VAR1)

Macro Modifiers

Replace all ".obj" in VAR1 with ".c":
VAR2 = $(VAR1,.obj=.c)
VAR4 = $(VAR3:foo%.obj=bar%.c)
24-11-2006 00.21

Invocation

Silent operation:
make -s
make TARGET:
make TARGET
06-08-2005 13.09

Example

# Enforce that $CC contains a valid compiler
ifndef CC
CC=g++
endif

# Some variables that our compiler understands
CFLAGS=-c -Wall -O2
LDFLAGS= -L/usr/X11R6/lib -lGLU -lGL -lglut -lm

# List of all source files
SOURCES=myprg.cpp file1.cpp file2.cpp

# Replace .cpp with .h the get the header files names
HEADERS=$(SOURCES:.cpp=.h)

# Replace .cpp with .o the get the object file names
OBJECTS=$(SOURCES:.cpp=.o)

# Name of our binary
EXECUTABLE=myprg_bin

.PHONY: all
all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS) $(HEADERS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

%.o: %.cpp
$(CC) $(CFLAGS) $< -o $@

.PHONY: clean
clean:
-rm *.o
-rm *~
-rm $(EXECUTABLE)
06-08-2005 13.09

Links

GNU MAKE
27-06-2006 07.04
Powered by PHP Created with Xemacs Valid XHTML 1.0! Valid CSS!