gcc 사용법
- gcc -v => 버전확인
- gcc temp.c => a.out 생성 ( # ./a.out )
- gcc -c temp.c => temp.o생성 //-c 컴파일 작업만 수행하는 옵션
- gcc -o temp temp.c => temp ( # ./temp ) // -o 옵션은 출력파일명을 정하는 옵션
- gcc -S temp.c => temp.s(역어셈코드생성)
최적화 옵션
- gcc -o test temp.c
- gcc -O4 -o test temp.c
- gcc -O2 -o test temp.c (커널, 임베디드는 O2단계를 Default로 사용)
- gcc -O1 -o test temp.c
매크로함수
geshi c
#include <stdio.h>
#include <stdlib.h>
#define fun(name0, name1, x, y) name0##_##name1(x, y)
// ##=> 붙여준다.
// add_int(x, y)
#define out_int(name0, name1, data) printf(#name0"_"#name1" : [%d ]\n", data)
#define out_float(name0, name1, data) printf(#name0"_"#name1" : [%f ]\n", data)
// #=>문자열을 만들어준다.
// printf("add""_""int"" : [%f ]\n", data) 문자열이 2개 중복으로 나올경우 삭제됨
// printf("add_int : [%f ]\n", data)
#define out(name0, name1, data) out_##name1(name0, name1, data)
int add_int(int x, int y) { return (x+y) ; }
int sub_int(int x, int y) { return (x-y) ; }
int mul_int(int x, int y) { return (x*y) ; }
int div_int(int x, int y) { return (x/y) ; }
float add_float(float x, float y) { return (x+y) ; }
float sub_float(float x, float y) { return (x-y) ; }
float mul_float(float x, float y) { return (x*y) ; }
float div_float(float x, float y) { return (x/y) ; }
void main()
{
int a = 100, b = 20, c ;
float fa = 50.2, fb = 20.4, fc = 0.0 ;
c = fun (add, int, a, b) ; out(add, int, c) ;
c = fun (div, int, a, b) ; out(div, int, c) ;
c = fun (mul, int, a, b) ; out(mul, int, c) ;
fc = fun(add, float, fa, fb) ; out(add, float, fc) ;
fc = fun(div, float, fa, fb) ; out(div, float, fc) ;
fc = fun(mul, float, fa, fb) ; out(mul, float, fc) ;
}
정적 라이브러리
- gcc -c test.c test01.c test02.c //
- gcc -c test test.c test01.c test02.c
- gcc -o test test.c test01.c test02.c -I../inc //-I 옵션은 include에서 지정한 헤더가 위치하는 폴더를 지정하는 옵션
- gcc -o test test.c test01.c test02.c -I/home/test/inc
- gcc -c test01.c test02.c -I../inc //-L 옵션은 라이브러리의 위치를 명시
- ar rs libtest.a test01.o test02.o //-l 옵션은 링크(link)할 라이브러리를 명시
- gcc -o test test.c -I../inc -L. -ltest (-L.은 경로명을 이야기하며 .은 현재 디렉토리, -ltest는 libtest.so 파일이나 sw,a(순서대로) 파일을 검색하여 라이브러리를 사용하도록 함)
동적 라이브러리
- gcc -fPIC -c test01.c test02.c -I../inc
- gcc -shared -W1,-soname,libtest.so.1 -o libtest.so.1.0.1 test01.o test02.0
- ln -s /home/test/lib/libtest.so.1.0.1 libtest.so
- gcc -o test test.c -I../inc ../lib/libtest.a
- gcc -o test test.c -I../inc -L../lib -ltest
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/test/lib
- vi /etc/ld.so.conf
- ldconfig
makefile
geshi c
INC=../inc
LIB=../lib
TYPE=a
FLAG=
NAME=
all : test.o libtest.$(TYPE) $(INC)/test.h
gcc -o test $(NAME) test.c -L$(IB) -ltest -I$(INC)
test.o : test.c $(INC)/test.h
gcc -c test.c -I$(INC)
libtest.so : test01.c test02.c $(INC)/test.h
gcc -fPIC -c test01.c test02.c -I$(INC)
gcc -shared -W1,-soname,libtest.so.1 -o libtest.so.1.0.1 test01.o test02.o
mv libtest.so.1.0.1 $(LIB)
ln -s /home/test/lib/libtest.so.1.0.1 $(LIB)/libtest.so
ls -al $(LIB)
libtest.a : test01.o test02.o
ar rs libtest.a test01.o test02.o
ar t libtest.a
mv libtest.a $(LIB)
test01.o : test01.c $(INC)/test.h
gcc -c test01.c -I$(INC)
test02.o : test02.c ../inc/test.h
gcc -c test02.c -I../inc
clean :
rm -rf *.o
rm -rf $(LIB)/*.*
중복된 작업을 순서대로 수행한다. 파일 수정 시간을 판단하여, 변화된 내용만 재컴파일한다.