Makefile 기초

숲사람·2022년 3월 24일
0

UNIX & C

목록 보기
6/12

Makefile 기본구조

 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 (Phony Targets 포니 타겟)

.PHONY 로 지정된 지시자는 명령을 나타냄.
예를 들어, 만약 해당 디렉터리에 clean이라는 파일이 있으면 make clean 은 동작하지 않음. clean은 디팬던시가 없기 때문에 항상 최신 파일이라고 여길것이기 때문. 따라서 이를 방지하기 위해 clean은 특수한 타겟이라고 지정해주는것.

참고 https://pinocc.tistory.com/131

profile
기록 & 정리 아카이브 용도 (보다 완성된 글은 http://soopsaram.com/documentudy)

0개의 댓글