target : dependency1 dependency2
command1
command2
$ make target
will issue command1 and command2, if dependency1 and dependency2 are exist.
.PHONY: all clean
CC = gcc
CFLAGS = -W
CFLAGS += -Wall # finally $(CFLAG) will be -W -Wall
all: hello
# make do the first line
# if there is no "hello" which is all's dependency, then goto hello:
hello.o: hello.c
$(CC) $(CFLAGS) -o $@ -c $^
# $@ target name, $^ dependencies
main.o: main.c hello.h
$(CC) $(CFLAGS) -c main.c
@echo "compile main.c"
hello: hello.o main.o
$(CC) $(CFLAGS) -o hello hello.o main.o
clean:
rm *.o
rm hello
.PHONY
로 지정된 지시자는 명령을 나타냄.
예를 들어, 만약 해당 디렉터리에 clean이라는 파일이 있으면 make clean
은 동작하지 않음. clean은 디팬던시가 없기 때문에 항상 최신 파일이라고 여길것이기 때문. 따라서 이를 방지하기 위해 clean은 특수한 타겟이라고 지정해주는것.