라이브러리 사용
program : quiz2.c
libsource : checkeod.c
libheader : libcheckeod.h
libname : libcheckeod.a
를 만들 것이다.
## -> 파일 이름
## checkeod.c
int checkeod(int x) {
if(x % 2 == 0)
return 0;
else
return 1;
}
## lib/libcheckeod.h
int checkeod(int);
# -> 우분트 서버에서 쓴 명령어
// -> 설명
# gcc -c checkeod.c //오브젝트 파일까지만 만든다. >> checkeod.o
# ar r libcheckeod.a checkeod.o
# ar rs libcheckeod.a
// checkeod.o를 기반으로 라이브러리를 만든다.
# mv libcheckeod.a lib
// lib 폴더로 옮기기
## quiz2.c // 홀수, 짝수 알려주는 프로그램
#include <stdio.h>
#include "libcheckeod.h"
void main() {
int n;
while (1) {
printf("Input number (0:Exit) : ");
scanf("%d", &n);
if(n == 0) {
printf("Program Exit~!!\n\n");
break;
} else {
if(checkeod(n) == 0)
printf("%d is even number~!!\n\n", n);
else
printf("%d is odd number~!!\n\n", n);
}
}
}
# gcc -o quiz2 quiz2.c -I ./lib -L ./lib -lcheckeod
// -o 옵션은 실행파일 이름을 지정 가능
// -I (대문자 i) 헤더가 호함된 디렉토리를 적어주는 옵션
// -l (소문자 l) 라이브러리의 이름을 적어주는 옵션
// -L 라이브러리가 포함된 디렉토리를 적어주는 옵션
# ./checkeod
실행
## Windows Terminal.
# scp xxx.pdf root@myu:/work/full/c/gcc
// 윈도우에 현재 디렉토리에 있는 파일 xxx.pdf를 우분트(myu) 해당 디렉토리로 복사
## ubuntu.
# tar cvzf quiz2_name.tar.gz quiz* check* lib/libcheck* xxx.pdf
// .gz형식으로 압축
## Windows Terminal.
# scp root@myu:/work/full/c/gcc/quiz2_name.tar.gz .
// 우분트의 파일을 현재 디렉토리로 복사
make 사용
### make 명령어
# mkdir -p /work/full/c/make
// 디렉토리 생성(-p 옵션은 중간 디렉토리 없을 시 생성)
# cd /work/full/c/make
// 이동
## test1.c
#include <stdio.h>
#include "a.h"
void func1();
void func2();
void main() {
printf("Test1\n");
func1();
func2();
}
## test2.c
#include <stdio.h>
#include "a.h"
#include "b.h"
extern void func1() {
printf("Test2\n");
}
## test3.c
#include <stdio.h>
#include "b.h"
#include "c.h"
extern void func2() {
printf("Test3\n");
}
## Makefile
test : test1.o test2.o test3.o
gcc -o test test1.o test2.o test3.o
test1.o : test1.c a.h
gcc -c test1.c
test2.o : test2.c a.h b.h
gcc -c test2.c
test3.o : test3.c b.h c.h
gcc -c test3.c
clean:
rm -rf test test*.o
# touch a.h b.h c.h
// touch는 파일을 생성하는 명령어
# make
// Makefile에 쓰여있는 대로 빌드
# ./test
# make clean
// 이전 빌드 결과 삭제
# make
// 빌드 과정을 재수행
# cp Makefile Makefile.1
// 카피
매크로: 파일 이름을 정의
ex) M_NAME = value
## Makefile 수정
// 매크로 추가
OBJF = test1.o test2.o test3.o
test : $(OBJF)
gcc -o test $(OBJF)
test1.o : test1.c a.h
gcc -c test1.c
test2.o : test2.c a.h b.h
gcc -c test2.c
test3.o : test3.c b.h c.h
gcc -c test3.c
clean: $(OBJF)
rm -rf test $(OBJF)
내부 매크로
$@ : 현재 목표 파일의 이름
$* : 확장자를 제외한 현재 목표 파일의 이름
$< : 현재 필수 조건 파일 중 첫 번째 파일 이름
$? : 현재 대상보다 최근에 변경된 필수 조건 파일 이름
$^ : 현재 모든 필수
## Makefile 수정
OBJF = test1.o test2.o test3.o
test: $(OBJF)
gcc -o $@ $^
test1.o : test1.c a.h
gcc -c $<
test2.o : test2.c a.h b.h
gcc -c $*.c
test3.o : test3.c b.h c.h
gcc -c $<
clean: $(OBJF)
rm -rf $^
# cp Makefile Makefile.3
# make clean
# make
## Makefile 수정
OBJF = test1.o test2.o test3.o
test: $(OBJF)
gcc -o $@ $^
clean: $(OBJF)
rm -rf $^
-> 중간에 c소스파일을 오브젝트 파일로 만드는 부분을 삭제
# cp Makefile Makefile.4
# make clean
# make
cc -c -o test1.o test1.c
cc -c -o test2.o test2.c
cc -c -o test3.o test3.c
rm -rf test1.o test2.o test3.o
-> gcc라고 안 나오고 cc라고 나옴
## Makefile 수정
OBJF = test1.o test2.o test3.o
test: $(OBJF)
gcc -o $@ $^
%.o : %.c
gcc -c $^
clean: $(OBJF)
rm -rf $^
%.o : %.c ->> 모든 c소스 파일을 모든 오브젝트 파일
# cp Makefile Makefile.5
# make clean
# make
gcc -c test1.c
gcc -c test2.c
gcc -c test3.c
gcc -o test test1.o test2.o test3.o
## Makefile 수정
OBJF = test1.o test2.o test3.o
test: $(OBJF)
gcc -o $@ $^
.c.o:
gcc -c $^
clean: $(OBJF)
rm -rf $^
%.o : %.c를 .c.o: 로 생략 가능
# cp Makefile Makefile.6
# make clean
# make
'Linux' 카테고리의 다른 글
Centos8 git push WARNING message 제거 (1) | 2024.03.14 |
---|---|
리눅스 Centos8 자바 설치 (0) | 2024.03.14 |
리눅스 3일차 (7) | 2024.03.12 |
리눅스 2일차 (0) | 2024.03.06 |
리눅스 1일차 정리 (0) | 2024.03.06 |