makefile
Syntax
target: prerequisites
command
target is object file, executable file, or a label
prerequisites are the files or targets to generate target
command is the commands will be implemented by make
Define variables
obj = main.o util.o
Compiler = gcc
hello: $(obj)
$(Compiler) $(obj)
#, comment
\, continue a line
Use include to include other makefiles
Support *, ? and [...]
Example 1
#ifndef UTIL_H
#define UTIL_H
void disp();
#endif
#include <stdio.h>
#include "util.h"
void disp()
{
printf("Hello World!\n");
}
#include "util.h"
int main()
{
disp();
return 0;
}
# comment line
objects = main.o util.o
hello.e: $(objects)
gcc -o hello.e $(objects)
main.o: main.c util.h
gcc -c main.c
util.o: util.c util.h
gcc -c util.c
clean: # no dependencies, not implemented automatically
rm *.o *.e
make or make hello.e, generate executable hello.e
make clean, remove *.o and *.e files
# comment line
objects = main.o util.o
hello.e: $(objects)
gcc -o hello.e $(objects)
main.o: util.h # "main.c" and "gcc -c main.c" will be derived automatically
util.o: util.h # "util.c" and "gcc -c util.c" will be derived automatically
.PHONY: clean
clean: # no dependencies, not implemented automatically
-rm -f *.o *.e # "-" before rm, continue to do further task not matter what happen
Static Pattern
- <targets ...>: <target-pattern>: <prereq-patterns ...>
- $^, a list of all the prerequisites of the rule
- $<, first prerequsite, *.c
- $@, targets, *.o
# comment line
objects = main.o util.o
cc = gcc
hello.e: $(objects)
gcc -o hello.e $(objects)
$(objects):%.o:%.c
$(cc) -c $< -o $@
.PHONY: clean
clean: # no dependencies, not implemented automatically
rm -f *.o *.e *~
Example 2
VPATH = src:include # search prerequisites in these directory
SRC = src
CFLAGS = -I include
objects = main.o util.o
cc = gcc
hello.e: $(objects)
$(cc) -o hello.e $(objects)
main.o: main.c util.h
$(cc) -c $(CFLAGS) $(SRC)/main.c
util.o: util.c util.h
$(cc) -c $(CFLAGS) $(SRC)/util.c
#vpath %.h include # user vpath to define the prerequisite search path
#vpath %.c src
.PHONY: clean
clean: # no dependencies, not implemented automatically
rm -f *.o *.e *~
# comment line
VPATH = src:include
CFLAGS = -I include
objects = main.o util.o
cc = gcc
hello.e: $(objects)
$(cc) -o hello.e $(objects)
$(objects):%.o:%.c
$(cc) -c $(CFLAGS) $< -o $@
.PHONY: clean
clean: # no dependencies, not implemented automatically
rm -f *.o *.e *~
Install/Uninstall Executable Program
VPATH = src:include
CFLAGS = -I include
objects = main.o util.o
cc = gcc
hello.e: $(objects)
$(cc) -o hello.e $(objects)
$(objects):%.o:%.c
$(cc) -c $(CFLAGS) $< -o $@
.PHONY: clean
clean: # no dependencies, not implemented automatically
rm -f *.o *.e *~
PREFIX = /usr/local
.PHONY: install
install: hello.e
mkdir -p $(PREFIX)/bin
cp $< $(PREFIX)/bin/hello
.PHONY: uninstall
uninstall:
rm -f $(PREFIX)/bin/hello
make install, install "hello" in /usr/local/bin
make PREFIX=./ install, install "hello" in the specified directory
make uninstall, remove "hello"
Install/Uninstall Library
# comment line
VPATH = src:include
SRC = src
LIB = lib
CFLAGS = -I include
objects = main.c util.o temp.o libhello.a
cc = gcc
hello.e: $(objects)
$(cc) -o hello.e $(CFLAGS) $< -L $(LIB) -lhello
libhello.a: util.o temp.o
ar rcs $(LIB)/libhello.a $^
temp.o: temp.c temp.h
$(cc) -c $(CFLAGS) $< -o $@
util.o: util.c util.h
$(cc) -c $(CFLAGS) $< -o $@
.PHONY: clean
clean: # no dependencies, not implemented automatically
rm -f *.o *.e *~ $(LIB)/*.a
PREFIX = /usr
alib = lib/libhello.a
.PHONY: install
install: $(alib)
mkdir -p $(PREFIX)/lib
mkdir -p $(PREFIX)/local/include
cp $(alib) $(PREFIX)/lib/
cp include/util.h $(PREFIX)/local/include/
cp include/temp.h $(PREFIX)/local/include/
.PHONY: uninstall
uninstall:
rm -f $(PREFIX)/$(alib)
rm -f $(PREFIX)/local/include/foo.h
rm -f $(PREFIX)/local/include/temp.h
make install, install "libhello.a" in /usr/lib
make PREFIX=temp install, install "libhello.a" in the specified directory
make uninstall, remove "libhello.a"
Reference