A target is usually a file (but compare with phony targets).
The commands must be prepended by a tab.
The first rule in a makefile is the default rule.
Targets
The macro $@ evaluates to the name of the current target.
foo: $(objs)
$(CXX) -o $@ $(objs)
If make is invoked without stating an explicit target, it tries to update the first target in the makefile.
Default goal
The first target that does not start with a dot is the default goal.
The default goal can be overridden
wth the .DEFAULT_GOAL special variable
by specifying the name of the goal(s) on the command line when invoking make
Suffix rules
Two types of suffix rules(?)
single suffix
double suffix
Example for double suffix rule:
.c.o:
$(CC) $(CFLAGS) -c $<
Suffix rules are obsoleted by pattern rules (which contain % signs).
The example above is equivalent to the following pattern rule:
%.o : %.c
$(CC) $(CFLAGS) -c $<
Macros
Macros seem to the make specific term for variables.
Macros can be defined when make is invoked:
$ make CFLAGS=-g
Multiple processors
With multiple processors (or cores), the compilation time can be reduced by parallelizing the tasks.
This can be achieved with export MAKEFLAGS='-j 2' or just make -j2.
The number of available processing units can be determined with nproc (MAKEFLAGS="-j $(nproc)").
Standard targets
all: Default target (same as just invoking make). Builds the executables, libraries, documentation etc.
install: install built things.
install-strip: like install, but additionally strip debugging symbols.
By default, make install will install the files in /usr/local/bin, /usr/local/lib etc. (That is: under prefix, which can be overwritten with ./configure[./configure --prefix=...`).
clean: opposite of make install
distclean: Also get rid of any files that ./configure has created.
check: run tests (if available)
installcheck: check the installed libraries.
dist: create PACKAGE-VERSION.tar.gz
distcheck: like dist with additional sanity checks. Prefer distcheck over dist.
maintainer-clean
Show make's default rules
The -p option prints the rules and variable values (referred to as database) that take effect with the given Makefile.
The -f option specifies the makefile (default is: Makefile), thus -f /dev/null makes sure that no Makefile is read.
# vim: ft=make
#
# Apparently, ifdef is not allowed outside of a target.
#
FOO=defined
VAR_F=FOO
VAR_B=BAR
all:
ifdef FOO
@echo FOO is defined
else
@echo FOO is not defined
endif
ifdef BAR
@echo BAR is defined
else
@echo BAR is not defined
endif
ifdef $(VAR_F)
@echo $(VAR_F) is defined
else
@echo $(VAR_F) is not defined
endif
ifdef $(VAR_B)
@echo $(VAR_B) is defined
else
@echo $(VAR_B) is not defined
endif
If a directory contains three c files: foo.c, bar.c and baz.c, then they can be compiled into the three executables foo, bar and baz with the following simple make file:
The GNU Make Standard Library s a collection of functions implemented using native GNU Make functionality that provide list and string manipulation, integer arithmetic, associative arrays, stacks, and debugging facilities.