1 / 18

Make

Make. H/W 이현종 2000. 3. 8. Make - C programming Utility. Make - Command generator. Instead of entering a great many compiler commands by hand, you use to make to automate the process. Ex) hyunjong : hyun.o jong.o gcc -o hyunjong hyun.o jong.o

noah
Télécharger la présentation

Make

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Make H/W 이현종 2000. 3. 8

  2. Make - C programming Utility Make - Command generator. Instead of entering a great many compiler commands by hand, you use to make to automate the process. Ex) hyunjong : hyun.o jong.o gcc -o hyunjong hyun.o jong.o hyun.o : io.h hyun.c gcc -c hyun.c jong.o : io.h jong.c gee -c jong.c

  3. Make의 구조 Target... : dependency... <Tab> command... Target(목표), dependency(의존관계), command(명령)의 세가지로 이루어진 기본적인 규칙들의 연속. 목표 : 명령이 수행되어서 나온 결과파일 (참고로 보통 make나 clean 과 같이 레이블기능을 제공하기도 한다. ) object file or executable file 의존관계 : 명령에 행하여질 파일에 관계하여 서로 의존관계에 있는 파일명이 온다. 명령 : 명령어가 오는 부분으로 일반적으로 쉘에서 쓸 수 있는 모든 명령어가 올 수 있다.

  4. Make예제 1 test : main.o read.o write.o gcc -o test main.o read.o write.o main.o : io.h main.c gcc -c main.c read.o : io.h read.c gcc -c read.c write.o :io.h write gcc -c write.c

  5. Make예제 2 - Macro, Label Macro로 바꾸어준다. OBJECTS = main.o read.o write.o test : $(OBJECTS) gcc -o $(OBJECTS) main.o : io.h main.c gcc -c main.c read.o : io.h read.c gcc -c read.c write.o : io.h write gcc -c write.c clean : rm $(OBJECTS) Macro는 $(…)에 채운다. Label의 경우 의존관계가 없어도 된다.

  6. Make - predefine macro ASFLAGS = Option AS =as CFLAGS= Option CC=cc CPPFLAGS= Option CXX=g++ LDLFAGS= Option LD=ld LFLAGS= Option LEX=lex YFLAGS= Option YACC=yacc MAKE_COMMAND=make

  7. Make 예제 3 - predefine macro OBJECTS = main.o read.o write.o SRCS = main.c read.c write.c CC = gcc CFLAGS= -g -c TARGET= test $(TARGET) : $(OBJECTS) $(CC) -o $(TARGET)$(OBJECTS) clean : rm -rf $(OBJECTS)$(TARGET)core main.o : io.h main.c read.o : io.h read.c write.o : io.h write.c -g : 디버그 -c : c파일을 컴파일함

  8. Make - 확장자 규칙(Suffix rule) .SUFFIXES = .c .o OBJECTS = main.o read.o write.o SRCS = main.c read.c write.c CC = gcc CFLAGS= -g -c INC = -I/home/raxis/include TARGET= test $(TARGET) : $(OBJECTS) $(CC) -o $(TARGET)$(OBJECTS) .c.o: $(CC)$(INC)$(CFLAGS)$< clean : rm -rf $(OBJECTS)$(TARGET)core main.o : io.h main.c read.o : io.h read.c write.o : io.h write.c .c를 이용하여 .o파일을 만든다. 확장자 규칙 : make가 어느 확장자를 가진 파일들을 처리할 것인가를 정의하는 것 확장자 규칙을 직접구현

  9. Make - 내부 macro & GNU에서의 .c.o 내부 Macro $@ -- 목표파일(Target). 콜론의 왼쪽에 오는 패턴을 치환 $< -- 입력파일. 콜론의 오른쪽에 오는 패턴을 치환. 최근 갱신된 파일 $* -- 확장자가 없는 현재의 입력 파일(Target) .c.o의 전통적인 표현 이외의 GNU에서의 확장문법 %.o : %.c gcc -c -o $@ $(CFLAGS) $< %_dbg.o : %.c gcc -c -o $@ $(CFLAGS) $< Test가 Target이라면 컴파일 된 Target은 Test_dbg.o가 된다.

  10. Macro substitution 필요에 의해 Macro의 내용을 조금만 바꾸고 싶을때 형식 $(macro_name:old=new) Ex) MY_NAME=michael jackson YOUR_NAME=$(MY_NAME:jackson=jack) OBJS=main.o read.o write.o SRCS=$(OBJS:.o=.c)

  11. Automatic dependency Target : dependency command 이 부분에서 command부분이 빠지게 되면 dependency를 나타내는 정보역할을 한다. Gccmakedep는 이러한 부분을 자동으로 생성하여 준다. - gcc -M XX.c 와 같은 의미이다. Ex) .SUFFIXES = .c .o CFLAGS= -O2 -g OBJS = main.o read.o write.o SRCS =$(OBJS:.o=.c) test:$(OBJS) $(CC) -o test $(OBJS) dep: gccmakedep$(SRCS)

  12. Multiple target Ex) * 결과파일이 여러 개 필요할 때 (make all - 실행시) .SUFFIXES = .c .o CC = gcc CFLAGS = -O2 -g OBJS1=main.o test1.o OBJS2=main.o test2.o OBJS3=main.o test3.o SRCS=$(OBJS1:.o=.c) $(OBJS2:.o=.c) $(OBJS3:.o=.c) all: test1 test2 test3 test1:$(OBJS1) $(CC) -o test1 $(OBJS1) test2:$(OBJS2) $(CC) -o test2 $(OBJS2) test3:$(OBJS3) $(CC) -o test3 $(OBJS3) dep: gccmakedep$(SRCS)

  13. Recursive make 여러 개의 maekfile을 동작시킬수 있다. 형식 1) subsystem: cd subdir; $(MAKE) 형식 2) subsystem: $(MAKE) -C subdir --- MAKE는 make의 매크로 형식1과 형식2는 동일하게 작동한다. Ex) ----------------------------------- 생략 ---------------------------------------- all : DataBase Test DataBase: cd db ; $(MAKE) Test : cd test ; $(MAKE) ----------------------------------- 생략 ----------------------------------------

  14. Tips 1) 긴 명령어를 잘라서 쓸 때… \를 이용한다. Ex) main.o:main.c \ /user/include/stdio.h\ /user/include/sys/cdefs.h 2) 불필요한 재컴파일 막기 make -t를 이용한다. T는 Touch를 의미하는 옵션으로 컴파일을 하지 않는 대신 파일의 생성 날짜만 가장 최근으로 바꾸어 놓는다.

  15. Make의 중요옵션 -C dir makefile을 읽지 말고 우선 dir로 이동하라는 뜻. -h 옵션에 관한 도움말을 출력한다. (help) -d makefile을 수행하는 동안의 각종 정보를 모조리 출력한다. 결과를 파일로 저장하여 보면 make의 동작을 알 수 있다. -f filename filename에 해당하는 파일을 makefile로 간주한다. -p make 에서 내부적으로 세팅되어 있는 값들을 출력한다. (-print -data -base)

  16. Makefile 의 예 .SUFFIXES = .c .o OBJS = object file 을 적어준다. SRCS = source파일을 적어준다. CC = gcc CFLAGS= -g $(INC) 컴파일에 필요한 옵션을 적어준다. INC = include되는 헤대파일의 패스를 적어준다. LIBS = 링크시 필요한 라이브러리를 추가한다. TARGET= 링크후 실행될 파일의 이름을 적어준다. all:$(TARGET) $(TARGET) : $(OBJS) $(CC) -o $@ $(OBJS) $(LIBS) .c.o: $(CC)$(INC)$(CFLAGS)$< dep: gccmakedep $(INC)$(SRCS) clean : rm -rf $(OBJECTS)$(TARGET)core

  17. Make the Library read.o write.o 를 libio.a라는 라이브러리로 만들어 본다. 만들기 위해서는 ar 유틸리티와 ranlib유틸리티가 필요하다. Ex) %ar rcv libio.a read.o write.o a -read.o 라이브러리에 추가 a -write.o %ranlib libio.a libio.a의 index를 생성 makefile을 이용한 라이브러리 만들기 Ex) TARGET = libio.a $(TARGET):$(OBJS) $(AR) rcv $@ $(OBJS) ranlib $@

  18. Make the dynamic (or shared) Library read.c write.c.를 컴파일 하여 libio.so.1을 만들어 본다. (so -> shared object , 1 ->동적라이브러리 버전 의미) Ex) %gcc -fPIC -c read.c %gcc -fPIC -c write.c %gcc -shared -W1m -soname, libio.so.1 -o libio.so.1 read.o write.o makefile 을 이용하기 Ex)-------------------------------------------생략------------------------------------------------ CFLAGS= -g $(INC) -fPIC TARGET=libio.so.1 all:$(TARGET) $(TARGET):$(OBJS) $(CC) -shared -W1, soname, $@ -o $@ $(OBJS) -------------------------------------------생략------------------------------------------------

More Related