Several make tricks

First of all, the manual is at the GNU site.

Setting a variable to the result of a shell operation: CURRENTDIR=$(shell pwd); or PLATFORM=$(shell uname).

Creating a directory including all intermediate ones: mkdir -p $(BUILD).

Simple suffix substitution in filenames: HFILES=$(CFILES:.cpp=.h); a more complex case (involving a change of directory), using foreach:

OFILES= $(foreach cfile,$(CFILES),$(BUILD)/$(cfile:.cpp=.o))

another way to do the same: TEXFILES= $(CFILES:%.cpp=$(DOCBUILD)/%.ctex).

Standard compilation rule:

%.o: %.cpp
    g++ $(OPTIONS) -c $< -o $@

Cleaning files without getting errors when they do not exist: -rm $(OFILES).

Suppressing output: @echo $(OFILES).

Including another Makefile: include ../Makefile.python.

Multiple targets: just list them to the left of ":".

Finally, escaping dollar signs: $$.

Dec 18, 2003

make (/progtricks)
2003-12-18, 14:35 [edit]

  Login:   Password:   [rss] [cc] [w3] [css]

© M. Vallisneri 2012 — last modified on 2010/01/29

Tantum in modicis, quantum in maximis