220 likes | 329 Views
[Unix Programming] gcc & make. Young-Ju, Han Email: yjhan@imtl.skku.ac.kr Website : http://imtl.skku.ac.kr/~yjhan. contents. gcc 기반 Complier and linking Make. Complier and linking. like.c source. like.i. cc1 C Complier. cpp0 or cpp preprocessor. Like.s. like. like.o. as
E N D
[Unix Programming]gcc & make Young-Ju, Han Email: yjhan@imtl.skku.ac.kr Website : http://imtl.skku.ac.kr/~yjhan
contents • gcc 기반 Complier and linking • Make 2005 UNIX Programming
Complier and linking like.c source like.i cc1 C Complier cpp0 or cpp preprocessor Like.s like like.o as assembler ld or collect2 linker gcc –v –save-temps –o like like.c gcc –v –l/myinclude1:/myinclude2 –DDEBUG –Wall –W –O2 –o like like.c -L/mylibdir1/mylibdir2 –lmylibname 2005 UNIX Programming
cpp or cpp0 Preprocessor • C Preprocessor command • %gcc –E –o like.i like.c • Function • File inclusion • -Idir option • Marco substitution • -Dmacro –Dmacro=defn –Umacro option • Conditional compile • #if #else #elif #ifdef #endif • Command • %gcc -o like –Idir –Dmacro –Umacro like.c 2005 UNIX Programming
cpp or cpp0 Preprocessor • error 1 #include <stdio.h> 2 #include <string. h> 3 #include “myinclude.h” 4 int main(void){ 5 int var1, var2; 6 var2=4; 7 strcpy(var1,var2); 8 var1 = var3()+var2; 9 printf("var1=%d %s\n", var1); 10 return(0); 11 } $gcc –E –o preproerror.i preproerror.c preproerror.c:3:23: myinclude.h: No such file or directory 2005 UNIX Programming
cc1 Compiler • C Compiler command • %gcc –S –o like.s like.c • Function • Parsing (Syntax Analysis) • -W -Wall option • Creating RTL(Register Transfer Language) • gcc –da like.c • Optimization • –O0, -O1, -O2, -O3, -Os option • -g, -g0, -g1, -g2, -g3 option • Creating object code • Command • %gcc -o like –Wall –Idir –Dmacro –Umacro –O3 like.c 2005 UNIX Programming
cc1 Compiler • error 1 #include <stdio.h> 2 #include <string. h> 3 int main(void){ 4 int var1, var2; 5 int a 6 var2=4; 7 strcpy(var1,var2); 8 var1 = var3()+var2; 9 printf("var1=%d\n", var1); 10 return(0); 11 } $gcc -W -Wall -S parsingerror.c parsingerror.c: In function `main': parsingerror.c:6: error: syntax error before "var2" parsingerror.c:7: warning: passing arg 1 of `strcpy' makes pointer from integer without a cast parsingerror.c:7: warning: passing arg 2 of `strcpy' makes pointer from integer without a cast parsingerror.c:8: warning: implicit declaration of function `var3' 2005 UNIX Programming
as Assembler • C Assembler command • %gcc –c like.c or gcc –c like.s • %as –o like.o hello.s 2005 UNIX Programming
ld or correct1 Linker • C Linker command • gcc –o like like.o • Function • Symbol reference resolving & location • Options • -Ldir (cf. LD_LIBRARY_PATH) • -llibname • -static • Static linking • Static library • Dynamic linking • Dynamic library 2005 UNIX Programming
ld or correct2 Linker • Static linking /usr/lib/libc.a like.o printf.o scanf.o read.o ld or correct2 ….. like.o printf.o scanf.o read.o ….. 2005 UNIX Programming
ld or correct2 Linker • error 1 #include <stdio.h> 2 #include <string. h> 3 int main(void){ 4 int var1, var2; 5 int a; 6 var2=4; 7 strcpy(var1,var2); 8 var1 = var3()+var2; 9 printf("var1=%d\n", var1); 10 return(0); 11 } $gcc -W -Wall –o linkingerror linkingerror.c Undefined first referenced symbol in file var3 /var/tmp//cc6XQGZ1.o ld: fatal: Symbol referencing errors. No output written to linkingerror collect2: ld returned 1 exit status' 2005 UNIX Programming
Making library file1.c mylib.h #include <stdio.h> void func1(){ printf(“Hello func1\n”); } void func2(){ printf(“Hello func2\n”); } void func1(); void func2(); void func3(); like.c #include <stdio.h> #include “mylib.h” int main() { func1(); func2(); func3(); } file2.c #include <stdio.h> void func3(){ printf(“Hello func3\n”); } 2005 UNIX Programming
Making library • static library • Compiling like.c and executing like $gcc –c file1.c file2.c $ar rscv libmy.a file1.o file2.o $gcc –o slike like.c –L./ -lmy $./like 2005 UNIX Programming
Making library • dynamic library • Compiling like.c and executing like $gcc –fPIC –c file1.c file2.c $gcc –shared –W1,-soname,libmy.so.0 –o libmy.so.0.0.0 file1.o file2.o $ln –s libmy.so.0.0.0 libmy.so # gcc 링크를 위한 파일 $ln –s libmy.so.0.0.0 libmy.so.0 # 동적 링크를 위한 파일 $setenv LD_LIBRARY_PATH mylibdir:$LD_LIBRARY_PATH $gcc –o dlike like.c –L./ -lmy $./like 2005 UNIX Programming
make • $ make all $make dynamic , $make static, $make clean… all : dlike slike dynamic : dlike static : slike slike : like.o gcc –static –W –Wall –o slike like.o –L./ -lmy dlike : like.o gcc–W –Wall –o slike like.o –L./ -lmy like.o : like.c gcc -W -Wall -c -o like.o like.c clean : rm –rf *.o 2005 UNIX Programming
make • 기술 파일의 기본 구조 • 비어있는 항은 무시한다 • #를 만나면 개행문자를 만날 때까지 무시한다 • 기술 행이 길어지면 ‘\’를 사용해서 이어나갈 수 있다. • ‘;’는 명령행을 나눌 때 사용한다. • 종속 항목이 없는 타겟도 사용가능하다. • 명령 부분에는 어떠한 명령어가 와도 상관 없다 CC = gcc target1 : dependency1 dependency2 < TAB >command1 command2 target2 : dependency3 dependency4 dependency5 command3 command4 Definition macro Rule 1 command Rule 2 command 2005 UNIX Programming
make • 매크로의 사용 • 매크로 참조 • 내부적으로 정의되어 있는 매크로 NAME = string #ok NAME = string #bad NAME:2=string #bad NAMe#2=string #bad NAME = string ${NAME} #string $(NAME) #string ${NAME}.c #string.c macro_${NAME} #macro_string CC #c compiler default : cc RM #remove file default : rm –f CFLAGS # c complier flags default : null 2005 UNIX Programming
make • 자동 매크로 리스트 $? #현재의 타겟보다 최근에 변경된 종속 항목 리스트 (확장자 규칙에서 사용불가) $^ #현재 타겟의 종속 항목 리스트(확장자 규칙에서 사용불가) $@ #현재 타겟의 이름 $< #현재 타겟보다 최근에 변경된 종속 항목 리스트(확장자 규칙에서만 사용가능) $* #현재 타겟보다 최근에 변경된 현재 종속 항목의 이름(확장자 규칙에서만 사용가능) diary : file1.o file2.o file3.o gcc –W –Wall –o diary file1.o file2.o file3.o file1.o : file1.c gcc –W –Wall –c –o file1.o file1.c file2.o : file2.c gcc –W –Wall –c –o file2.o file2.c file2.o : file2.c gcc –W –Wall –c –o file2.o file3.c 2005 UNIX Programming
make • 자동 매크로 적용 CC = gcc CFLAGS = -W –Wall diary : file1.o file2.o file3.o $(CC) $(CFLAGS) –o $@ $^ file1.o : file1.c $(CC) $(CFLAGS) –c -o $@ $^ file2.o : file2.c $(CC) $(CFLAGS) –c -o $@ $^ file2.o : file2.c $(CC) $(CFLAGS) –c -o $@ $^ 2005 UNIX Programming
make • 확장자 규칙 (make –p) • 적용 예 %.o: %.c # commands to execute (built-in): $(COMPILE.c) $(OUTPUT_OPTION) $< # cc –c –o $@ $< .c.o : $(COMPILE.c) $(OUTPUT_OPTION) $< # cc –c –o $@ $< CC = gcc CFLAGS = -W –Wall diary : file1.o file2.o file3.o $(CC) $(CFLAGS) –o $@ $^ 2005 UNIX Programming
make • 확장자 규칙 확장 CC = gcc CFLAGS = -W –Wall .SUFFIZES : .o .c %.o: %.c $(CC) $(CFLAGS) –DDEBUG –c –o $@ $< diary : file1.o file2.o file3.o $(CC) $(CFLAGS) –o $@ $^ CC = gcc CFLAGS = -W –Wall .SUFFIZES : .o .c .c.o : $(CC) $(CFLAGS) –DDEBUG –c –o $@ $< diary : file1.o file2.o file3.o $(CC) $(CFLAGS) –o $@ $^ 2005 UNIX Programming
make • 와일드카드 매칭 기법과 대입 참조 기법 CC = gcc CFLAGS = -W –Wall SRCS = file1.c file2.c file3.c SRCS = $(wildcard *.c) #file1.c file2.c file3.c OBJS = $(SRCS:.c=.o) #file1.o file2.o file3.o .c.o: $(CC) $(CFLAGS) –DDEBUG –c –o $@ $< diary : $(OBJS) $(CC) $(CFLAGS) –o $@ $^ 2005 UNIX Programming