$ gcc [options] filename
- Options
- -c: object file(.o)만 생성
- -o: execution file name 지정 (default: a.out)
$ gcc test.c
$ ls
a.out test.c
$ gcc -o test test.c
$ ls
test test.c
# 오브젝트 파일은 실행 파일이 아니기 때문에 실행 안됨
$ gcc -c -o test.o test.c
$ ls
test.o test.c
CC=gcc # 컴파일러 이름
OFLAGS=-g -Wall # 옵션
OBJS=main.o foo.o bar.o
TARGET=app.out
$(TARGET): $(OBJS)
$(CC) -o $@ $(OBJS) # $@ : $(TARGET) 정보가 자동으로 들어간다.
main.o: foo.h bar.h main.c
foo.o: foo.h foo.c
bar.o: bar.h bar.c
Clean rule
Build로 생성된 파일들 삭제하는 규칙
Clean build를 위해 사용 가능
clean:
rm -f *.o
rm -f $(TARGET)
$ make clean
Make file Basic pattern
CC=<compiler>
CFLAGS=<options for compiler>
LDFLAGS=<options for linker>
LDLIBS=<a list of library to link>
OBJS=<a list of object file>
TARGET=<build target name>
all: $(TARGET)
clean:
rm -f *.o
rm -f $(TARGET)
$(TARGET): $(OBJS)
$(CC) -o $@ $(OBJS)
파일 개요 & 기본 명령어 (파일 입출력 1/4)
File
File 이란?
보조 기억 장치에 저장된 연관된 정보들의 집합
보조 기억 장치 할당의 최소 단위
Sequence of bytes (물리적 정의)
OS는 file operation들에 대한 system call을 제공해야 함
create, write, read, reposition, delete, Etc..
파일의 종류
Regular file (일반 파일)
Text or binary data file
Directory
Unix/Linux에서는 directory도 하나의 파일
Special file (특수 파일)
파일 형태로 표현된 커널 내 객체
데이터 전송, 장치 접근 시 사용하는 파일
기본 명령어 (생략)
File access permission (생략)
File I/O
Low-Level File IO (system call)
system call을 이용해서 파일 입출력 수행
File descriptor 사용
Byte 단위로 디스크에 입출력
특수 파일에 대한 입출력 가능
High-Level File IO (Buffered IO)
C Standard library를 사용해서 파일 입출력 수행
File pointer 사용
버퍼(block) 단위로 디스크에 입출력
open (system call)
$ man -s 2 open
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
pathname (file path)
열려는 파일의 경로 (파일 이름 포함)
flags (file state flags)
파일을 여는 방법(access mode) 설정
mode (file access permission)
파일을 새로 생성(O_CREATE) 할 때만 유효
Return: file descriptor
File descriptor
열려 있는 파일을 구분하는 정수(integer) 값
특수 파일 등 대부분의 파일을 지칭 가능
Process별로 kernel이 관리
파일을 열 때 순차적으로 할당 됨
Process 당 최대 fd 수 = 1,024 (default, 변경 가능)
Default fds (수정 가능)
0: stdin
1: stdout
2: stderr
flags (Man page 및 <sys/fcntl.h> 참조)
여러 플래그 조합 가능 (OR bit operation (|) 사용)
read, write, RW
File table
열린 파일을 관리하는 표
kernel이 process 별로 유지
열린 파일에 대한 각종 정보 관리
Access mode, file offset, pointer to files
mode (Man page 및 <sys/stat.h> 참조)
파일 권한 설정 값 사용 (예, 644)
정의 된 플래그 사용
조합하여 사용 가능 (OR bit operation (|) 사용
조합 사용법
flag 조합의 예
O_WRONLY | O_TRUNC
O_RDWR | O_APPEND
mode 조합의 예
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
close (system call)
$ man -s 2 close
#include <unistd.h>
int close(int fd);