화요일, 9월 29, 2009

리눅스 C Compile 및 Make 사용법

이동: Home à os0103

 

주소: http://www.kernel.kr/os/01/os0103.htm

 

제목: 리눅스 C Compile 및 Make 사용법

저작권은 저자에게 있습니다

저자: 정재준(rgbi3307@nate.com)

최근수정일:2008-12-13

 

 

리눅스 C Compile 및 Make 사용법

1.       C 컴파일 방법

2.       라이브러리 생성 및 사용법

3.       Make 사용법

 

1. C 컴파일 방법

먼저, 아래와 같이 간단한 C 코드를 작성한다.

 

파일명: hello.c

 

#include

 

int main ()

{

    printf (“Hello Worldn”);

    exit(0);

}

 

위의 파일을 컴파일, 링크하여 실행한다.

$ cc –c hello.c  ß 컴파일

$ ll

 

-rw-rw-r--    1 jungjj   jungjj         79 12월 13 08:41 hello.c

-rw-rw-r--    1 jungjj   jungjj        840 12월 13 08:49 hello.o  ß object 코드 생성됨

 

$ cc -o hello hello.o  ß 링크

$ ll

 

-rwxrwxr-x    1 jungjj   jungjj      11638 12월 13 08:51 hello  ß 실행파일 생성됨

-rw-rw-r--    1 jungjj   jungjj         79 12월 13 08:41 hello.c

-rw-rw-r--    1 jungjj   jungjj        840 12월 13 08:49 hello.o

 

$ ./hello  ß 실행

Hello World

 

아래와 같이 컴파일과 링크를 한꺼번에 하여 실행파일을 생생할 수도 있다.

$ cc –o hello hello.c  ß 컴파일 및 링크하여 실행파일 생성

 

 

 

2. 라이브러리 생성 및 사용법

이번에는 C 코드파일을 여러 개 만들어서 object 코드를 생성하고 링킹하는 방법이다.  또한 서브코드를 라이브러리로 생성하여 링킹하는 방법에 대해서도 알아보자.

 

먼저, 아래와 같이 2개의 서브코드을 생성한다.

 

파일명: bill.c

#include

 

void bill (char* arg)

{

    printf (“bill: %sn”, arg);

}

 

파일명: fred.c

#include

 

void fred (char* arg)

{

    printf (“fred: %sn”, arg);

}

 

위의 서브코드를 컴파일하여 object 코드를 생성한다.

$ cc –c fred.c bill.c

$ ll *.o

 

-rw-rw-r--    1 jungjj   jungjj        788 12월 13 09:21 bill.o

-rw-rw-r--    1 jungjj   jungjj        788 12월 13 09:21 fred.o

 

 

위의 서브코드를 메인코드에서 사용하기 위하여, 헤더파일에 선언한다.

 

파일명: lib.h

 

void fred (char*);

void bill (char*);

 

 

메인코드에서 위의 헤더파일을 포함시키고, 서브코드 함수를 호출한다.

 

파일명: program.c

#include “lib.h”

 

int main ()

{

    bill (“hello”);

    fred (“hello”);

    exit(0);

}

 

메인코드를 컴파일하고, 서브코드 object와 링크하여 실행파일을 만든다.

 

$ cc -c program.c

$ cc -o program program.o bill.o fred.o

$ ll

 

-rw-rw-r--    1 jungjj   jungjj         78 12월 13 09:20 bill.c

-rw-rw-r--    1 jungjj   jungjj        788 12월 13 09:21 bill.o

-rw-rw-r--    1 jungjj   jungjj         82 12월 13 09:15 fred.c

-rw-rw-r--    1 jungjj   jungjj        788 12월 13 09:21 fred.o

-rw-rw-r--    1 jungjj   jungjj         41 12월 13 09:27 lib.h

-rwxrwxr-x    1 jungjj   jungjj      11912 12월 13 09:28 program  ß 실행파일 생성됨

-rw-rw-r--    1 jungjj   jungjj         78 12월 13 09:27 program.c

-rw-rw-r--    1 jungjj   jungjj        884 12월 13 09:28 program.o

$ ./program  ß 실행

bill: hello

fred: hello

 

 

이번에는 ar 프로그램을 사용하여 서브코드를 라이브러리 파일로 생성해 보자.  이때 ar 프로그램은 object 파일들을 합하여 archive 파일을 생성하는 역할을 한다.

 

$ ar crv lib_billfred.a bill.o fred.o

a - bill.o

a - fred.o

$ ll

 

-rw-rw-r--    1 jungjj   jungjj         78 12월 13 09:20 bill.c

-rw-rw-r--    1 jungjj   jungjj        788 12월 13 09:21 bill.o

-rw-rw-r--    1 jungjj   jungjj         82 12월 13 09:15 fred.c

-rw-rw-r--    1 jungjj   jungjj        788 12월 13 09:21 fred.o

-rw-rw-r--    1 jungjj   jungjj         41 12월 13 09:27 lib.h

-rw-rw-r--    1 jungjj   jungjj       1786 12월 13 09:34 lib_billfred.a  ß 라이브러리 파일 생성

-rwxrwxr-x    1 jungjj   jungjj      11912 12월 13 09:28 program

-rw-rw-r--    1 jungjj   jungjj         78 12월 13 09:27 program.c

-rw-rw-r--    1 jungjj   jungjj        884 12월 13 09:28 program.o

 

위에서 생성한 라이브러리 파일을 링크하여 실행파일을 생성하여 실행한다.

 

$ cc –o program program.o lib_billfred.a

$ ./program

bill: hello

fred: hello

 

 

위의 링크명령을 아래와 같이 사용해도 된다.  여기서 –L.는 라이브러리 파일의 위치가 현재 폴더에 있다는 의미이고, -l_billfred는 라이브러리 파일명을 의미한다.

 

$ cc -o program program.o -L. -l_billfred

$ ./program

bill: hello

fred: hello

 

 

리눅스에서는 사용하는 파일과 DOS 파일 명칭을 비교하면 아래와 같다.

 

파일구분

Linux/Unix

DOS

Object 모듈

func.o

FUNC.OBJ

공유 라이브러리

lib.so

LIB.DLL

정적 라이브러리

lib.a

lib.sa

LIB.LIB

실행 프로그램

program

PROGRAM.EXE

 

 

3. Make 사용법

이번에는 위에서 실습한 코드들을 make 파일을 사용하여 빌드해 보자.  먼저 아래와 같이 make 파일을 생성한다.

 

파일명: make_file1

myapp: program.o bill.o fred.o

         gcc -o myapp program.o bill.o fred.o

 

program.o: program.c lib.h

         gcc -c program.c

        

bill.o: bill.c

         gcc -c bill.c

        

fred.o: fred.c

         gcc -c fred.c

 

make 프로그램을 사용하여 위의 파일을 실행한다.

 

$ make -f make_file1

gcc -o myapp program.o bill.o fred.o

$ ll

 

-rw-rw-r--    1 jungjj   jungjj         78 12월 13 09:20 bill.c

-rw-rw-r--    1 jungjj   jungjj        788 12월 13 09:21 bill.o

-rw-rw-r--    1 jungjj   jungjj         82 12월 13 09:15 fred.c

-rw-rw-r--    1 jungjj   jungjj        788 12월 13 09:21 fred.o

-rw-rw-r--    1 jungjj   jungjj         41 12월 13 09:27 lib.h

-rw-rw-r--    1 jungjj   jungjj       1786 12월 13 09:34 lib_billfred.a

-rw-rw-r--    1 jungjj   jungjj        179 12월 13 10:08 make_file1

-rwxrwxr-x    1 jungjj   jungjj      11912 12월 13 10:09 myapp  ß 실행파일 생성됨

-rwxrwxr-x    1 jungjj   jungjj      11912 12월 13 09:41 program

-rw-rw-r--    1 jungjj   jungjj         78 12월 13 09:27 program.c

-rw-rw-r--    1 jungjj   jungjj        884 12월 13 09:28 program.o

 

$ ./myapp  ß 실행

bill: hello

fred: hello

 

지금까지 리눅스 환경에서 C 코드파일을 컴파일하고 링크하여 실행파일을 생성하는 방법을 간단히 기술 하였다.  아울러 make 파일을 통하여 빌드하는 방법에 대해서도 언급 하였다.

 

 

 

이동: Home à os0103

 

주소: http://www.kernel.kr/os/01/os0103.htm

 

제목: 리눅스 C Compile 및 Make 사용법

저작권은 저자에게 있습니다

저자: 정재준(rgbi3307@nate.com)

최근수정일:2008-12-13

 


댓글 없음: