1 / 24

제 9 장 C 프로그래밍 환경

제 9 장 C 프로그래밍 환경. TOPCIT (Test of Practical Competency in IT) IT 역량지수평가 http://topcit.or.kr/ 리눅스마스터 국가공인자격증 http :// exam.ihd.or.kr/ 1 차신청 : 10.07 ~ 11.12, 1 차시험 10.08 ~ 11.13 2 차신청 : 10.08~12.13, 2 차시험 : 12.7 ). 9.1 컴파일러. Programs translated by other programs

morey
Download Presentation

제 9 장 C 프로그래밍 환경

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. 제9장 C 프로그래밍 환경 TOPCIT (Test of Practical Competency in IT)IT역량지수평가http://topcit.or.kr/ 리눅스마스터국가공인자격증 http://exam.ihd.or.kr/ 1차신청: 10.07 ~ 11.12, 1차시험10.08 ~ 11.13 2차신청: 10.08~12.13, 2차시험: 12.7)

  2. 9.1 컴파일러

  3. Programstranslatedbyotherprograms (Computer Systems: A Programmer’s Perspective, Randal E. Bryant and David R. O’Hallaron, Prentice Hall, 2011. p5) $ ar –t /usr/lib/i686-redhat-linux5E/lib/libc.a | grepprintf.o unix>gcc–ohellohello.c printf.o Pre- processor (cpp) Assembler (as) Linker (ld) Compiler (cc1) hello.c Source program (text) hello.i Modified source program hello Executable object program (binary) hello.S Assemblyprogram (text) hello.o Relocatable object programs (binary) • 어셈블 (assemble) • $ gcc –S hello.c • 역어셈블러(deassemble) • $ gcc –c hello.c • $ objdump –d hello.o • Pre-processing • E.g.,#include<stdio.h>isinsertedintohello.i • Compilation(.S) • Each statement is an assembly language program • Assembly(.o) • Abinaryfilewhosebytesencodemach.languageinstructions • Linking • Getprintf()whichresidesinaseparateprecompiledobjectfile 13

  4. 단일 모듈 프로그램: longest.c #include <stdio.h> #define MAXLINE 100 void copy(char from[], char to[]); char line[MAXLINE]; // 입력 줄 char longest[MAXLINE]; // 가장 긴 줄 /*입력 줄 가운데 가장 긴 줄 프린트 */ main() { intlen; int max; max = 0; while (gets(line) != NULL) { len = strlen(line); if (len > max) { max = len; copy(line, longest); } } if (max > 0) // 입력 줄이 있었다면 printf("…The logest…\n%s\n", longest); return 0; } /*copy: from을 to에 복사; to가 충분히 크다고 가정*/ void copy(char from[], char to[]) { inti; i = 0; while ((to[i] = from[i]) != '\0') ++i; } debugging: strlen() debugging: gets()

  5. gcc 컴파일러 • gcc(GNU cc) 컴파일러상업용 C 컴파일러(cc) $ gcc [-옵션] 파일 $ cc [-옵션] 파일 • 컴파일 $ gcclongest.c $ a.out // 실행 • -c 옵션 $ gcc -c longest.c • -o 옵션 $ gcc -o longest longest.o 혹은 $ gcc -o longest longest.c $ longest // 실행

  6. 다중 모듈 프로그램 • 단일 모듈 프로그램 • 코드의 재사용(reuse)이 어렵고, • 여러 사람이 참여하는 프로그래밍이 어렵다 • 예를 들어 다른 프로그램에서 copy 함수를 재사용하기 힘들다 • 다중 모듈 프로그램 • 여러 개의 .c 파일들로 이루어진 프로그램 • 일반적으로 복잡하며 대단위 프로그램인 경우에 적합

  7. 다중 모듈 프로그램: main & copy • main 프로그램과 copy 함수를 분리하여 별도 파일로 작성 • main.c • copy.c • copy.h // 함수의 프로토타입을 포함하는 헤더 파일 • 컴파일 $ gcc -c main.c $ gcc -c copy.c $ gcc -o main main.o copy.o 혹은 $ gcc -o main main.c copy.c

  8. main.ccopy.c #include <stdio.h> #include <string.h> #include "copy.h" char line[MAXLINE]; // 입력 줄 char longest[MAXLINE]; // 가장 긴 줄 /*입력 줄 가운데 가장 긴 줄 프린트 */ main() { intlen; int max; max = 0; while (fgets(line, MAXLINE, stdin) != NULL) { len = strlen(line); if (len > max) { max = len; copy(line, longest); } } if (max > 0) // 입력 줄이 있었다면 printf("…The logest…\n%s\n", longest); return 0; } #include "copy.h" /* copy: from을 to에 복사; to가 충분히 크다고 가정*/ void copy(char from[], char to[]) { inti; i = 0; while ((to[i] = from[i]) != '\0') ++i; } copy.h #define MAXLINE 100 void copy(char from[], char to[]);

  9. 9.2 make 시스템

  10. make 시스템의 필요성 • 다중 모듈 프로그램을 구성하는 일부 파일이 변경된 경우? • 변경된 파일만 컴파일하고, 파일들의 의존 관계에 따라서 필요한 파일만 다시 컴파일하여 실행 파일을 만들면 좋다. • 예 • copy.c소스 코드를 수정 • 목적 파일 copy.o 생성 • 실행파일을 생성 • make 시스템 • 대규모 프로그램의 경우에는 헤더, 소스 파일, 목적 파일, 실행 파일의 모든 관계를 기억하고 체계적으로 관리하는 것이 필요 • make 시스템을 이용하여 효과적으로 작업

  11. 메이크파일(Makefile) • 메이크파일 • 실행 파일을 만들기 위해 필요한 파일들과 그들 사이의 의존 관계, 만드는 방법 등을 기술 • make 시스템은 메이크파일을 이용하여 파일의 상호 의존 관계를 파악하여 실행 파일을 쉽게 다시 만듦 • $ make [-f 메이크파일] • 옵션이 없으면 Makefile혹은 makefile을 사용

  12. 메이크파일의 구성 • 메이크파일의 구성 형식 대상리스트: 의존리스트 명령리스트 • 메이크파일예 $ vi makefile main: main.ocopy.o gcc -o main main.ocopy.o main.o: main.ccopy.h gcc -c main.c copy.o: copy.ccopy.h gcc -c copy.c $ make–f makefile • 매크로(Macros) • make에서 지원하는 기능: make 파일 내에 모든 $(token)는 replacementText로 대치 • token = replacementText • 규칙 .c.o: $(CC) $(CFLAGS) $(CPPFLAGS) -c $< CFLAGS= -O (optimize) (예) CFLAGS= -g (gdb를 위한 디버깅 정보 포함) –p (모든 매크로 정의 출력) • 그 외 정보들: $ man make • make 실행 $ make 혹은 $ make main gcc -c main.c gcc -c copy.c gcc -o main main.ocopy.o • copy.c파일이 변경된 후 $ touch copy.c $ make gcc -c copy.c gcc -o main main.ocopy.o

  13. 9.3 디버거

  14. gdb • 가장 대표적인 디버거 • GNU debugger(gdb) • gdb주요 기능 • 정지점(breakpoint) 설정 • 한 줄씩 실행 • 변수 접근 및 수정 • 함수 탐색 • 추적(tracing) • gdb사용을 위한 컴파일 • -g 옵션을 이용하여 컴파일 $ gcc -g -o longest longest.c • 다중 모듈 프로그램 $ gcc -g -o main main.ccopy.c • gdb실행 $ gdb [실행파일] $ gdb main (gdb) help (gdb) help running (gdb) help data

  15. gdb 기능 • 소스보기 : l(ist) • l [줄번호] 지정된 줄을 프린트 • l [파일명]:[함수명] 지정된 함수를 프린트 • set listsize n 출력되는 줄의 수를 n으로 변경 (gdb) l copy 1 #include <stdio.h> 2 #include "copy.h" 3 /* copy: from을 to에 복사; to가 충분히 크다고 가정*/ 4 void copy(char from[], char to[]) 5 { 6 inti; 7 i = 0; 8 while ((to[i] = from[i]) != '\0') 9 ++i; 10 }

  16. gdb 기능 • 정지점: b(reak), clear, d(elete) • b [파일:]함수 파일의 함수 시작부분에 정지점 설정 • b n n번 줄에 정지점을 설정 • b +n 현재 줄에서 n개 줄 이후에 정지점 설정 • b -n 현재 줄에서 n개 줄 이전에 정지점 설정 • info b 현재 설정된 정지점을 출력 • clear 줄번호해당 정지점을 삭제 • d 모든 정지점을 삭제 (gdb) b 13 Breakpoint 1 at 0x8048455: file main.c, line 13. (gdb) b copy Breakpoint 2 at 0x80484da: file copy.c, line 6. (gdb) info b Num Type DispEnb Address What 1 breakpoint keep y 0x08048455 in main at main.c:13 2 breakpoint keep y 0x080484da in copy at copy.c:6

  17. gdb기능 • 프로그램 수행 : r(un), n(ext), c(ontinue) • r(un) 인수 명령줄 인수를 받아 프로그램 수행 • k(ill) 프로그램 수행 강제 종료 • n(ext) 멈춘 지점에서 다음 줄을 수행하고 멈춤 • s(tep) n과 같은 기능 함수호출 시 함수내부로 진입 • c(ontinue) 정지점을 만날 때까지 계속 수행 • u 반복문에서빠져나옴 • finish 현재 수행하는 함수의 끝으로 이동 • return 현재 수행중인 함수를 빠져나옴 • quit 종료 • 변수 값 프린트 : p(rint) • p [변수명] 해당 변수 값 프린트 • p 파일명::[변수명] 특정 파일의 전역변수 프린트 • p [함수명]::[변수명] 특정 함수의 정적 변수 프린트 • info locals 현재 상태의 지역변수 리스트 • 역어셈블 • disas(semble) 지정된 메모리 섹션을 역어셈블

  18. gdb 기능 (gdb) s 8 ++i; (gdb) p to $13 = 0x80497c0 "M" (gdb) s 7 while ((to[i] = from[i]) != '\0') (gdb) s 8 ++i; (gdb) p to $14 = 0x80497c0 "Me" (gdb) c Continuing. Happy New Year ! Breakpoint 2, copy (from=0x8049840 "Happy New Year !\n", to=0x80497c0 "Merry X-mas !\n") at copy.c:6 6 i = 0; (gdb) s 7 while ((to[i] = from[i]) != '\0') (gdb) s 8 ++i; (gdb) p to $15 = 0x80497c0 "Herry X-mas !\n" (gdb) c Continuing. ...The logest... Happy New Year ! Program exited normally. (gdb) quit (gdb) r Starting program: /home/mysung/ulprog/cprog/main Breakpoint 1, main () at main.c:13 13 while (fgets(line, MAXLINE, stdin) != NULL) { (gdb) s Merry X-mas ! 14 len = strlen(line); (gdb) s 15 if (len > max) { (gdb) s 16 max = len; (gdb) p len $10 = 14 (gdb) s 17 copy(line, longest); (gdb) s Breakpoint 2, copy (from=0x8049840 "Merry X-mas !\n", to=0x80497c0 "") at copy.c:6 6 i = 0; (gdb) s 7 while ((to[i] = from[i]) != '\0') (gdb) p from $11 = 0x8049840 "Merry X-mas !\n" (gdb) p to $12 = 0x80497c0 "" (gdb) s 8 ++i; (gdb) p to $13 = 0x80497c0 "M"

  19. DDD(Data Display Debugger) • gdb를 위한 그래픽 사용자 인터페이스 • http://www.gnu.org/software/ddd • 정지점을 설정 • 소스코드의 원하는 위치에 커서를 이동하고 상 Break 버튼 • Next나 Step 같은 명령어 버튼을 이용하여 한 줄씩 실행 • 하단에는 gdb명령어 입력 창

  20. DDD(Data Display Debugger) • Next: 한 줄 진행 • Up: 함수 호출자 보기 • Cont: 계속 진행

  21. (C언어 보충) main() 함수의 숨겨진 이야기 • echo.c코딩 #include <stdio.h> main(intargc, char *argv[]) { inti; for (i = 1; i < argc; i++) printf("%s%s", argv[i], (i < argc-1) ? " " : ""); printf("\n"); return 0; } • echo.c컴파일 & 실행 $ gccecho.c –o echo $ ./echo hello world! argc: 3 argv: 0 1 2 3 ./echo\0 hello\0 world!\0 0

  22. (실습&제출) myecho.c코딩 & 컴파일 & 실행 (응용) myecho.c 작성: 명령라인에서 $ myechohello world! 를 실행하면 argc와argv[]의 값과 실행결과를 모두출력하도록 echo.c프로그램을 수정하세요 • Command $ gcc –o myechomyecho.c $ myechohello world! Command • Output argc=3 argv[0]=“myecho” argv[1]=“hello” argv[2]=“world!” hello world! argc: 3 argv: 0 1 2 3 myecho\0 hello\0 world!\0 0

  23. $ gdb echo (1) $ gcc –g –o echo echo.c $ gdb ./echo (gdb) l 1 #include <stdio.h> 2 main(int argc, char *argv[]) 3 { 4 int i; 5 for (i = 1; i < argc; i++) 6 printf("%s%s", argv[i], (i < argc-1) ? " " : ""); 7 printf("\n"); 8 return 0; 9 } 10 (gdb) b 6 Breakpoint 1 at 0x8048407: file echo.c, line 6. (gdb) i b Num Type Disp Enb Address What 1 breakpoint keep y 0x08048407 in main at echo.c:6 (gdb) info frame No stack.

  24. $ gdb echo (2) (gdb) p &argv[2] $9 = (char **) 0xbffff6bc (gdb) p &argv[3] $10 = (char **) 0xbffff6c0 (gdb) p *(&argv[0]) $11 = 0xbffff801 "/home/mysung/ulprog/echo" (gdb) p *(&argv[1]) $12 = 0xbffff81a "hello" (gdb) p *(&argv[2]) $13 = 0xbffff820 "world!" (gdb) p *(&argv[3]) $14 = 0x0 (gdb) p i $15 = 1 (gdb) p argv[i] $16 = 0xbffff81a "hello" (gdb) c Continuing. Breakpoint 1, main (argc=3, argv=0xbffff6b4) at echo.c:6 6 printf("%s%s", argv[i], (i < argc-1) ? " " : ""); (gdb) p i $17 = 2 (gdb) p argv[i] $18 = 0xbffff820 "world!" (gdb) c Continuing. hello world! [Inferior 1 (process 6893) exited normally] (gdb) r hello world! Starting program: /home/mysung/ulprog/echo hello world! Breakpoint 1, main (argc=3, argv=0xbffff6b4) at echo.c:6 6 printf("%s%s", argv[i], (i < argc-1) ? " " : ""); (gdb) i f Stack level 0, frame at 0xbffff620: eip = 0x8048407 in main (echo.c:6); saved eip 0x49aa33f3 source language c. Arglist at 0xbffff618, args: argc=3, argv=0xbffff6b4 Locals at 0xbffff618, Previous frame's sp is 0xbffff620 Saved registers: ebp at 0xbffff618, eip at 0xbffff61c (gdb) p &argc $1 = (int *) 0xbffff620 (gdb) p &argv $2 = (char ***) 0xbffff624 (gdb) p argv[0] $3 = 0xbffff801 "/home/mysung/ulprog/echo" (gdb) p argv[1] $4 = 0xbffff81a "hello" (gdb) p argv[2] $5 = 0xbffff820 "world!" (gdb) p argv[3] $6 = 0x0 (gdb) p &argv[0] $7 = (char **) 0xbffff6b4 (gdb) p &argv[1] $8 = (char **) 0xbffff6b8 http://ko.wikipedia.org/wiki/IA-32#.EC.9D.BC.EB.B0.98_.EB.A0.88.EC.A7.80.EC.8A.A4.ED.84.B0

More Related