Login | Join | OpenID | RSS Feed
hello.c 예제
최광용 Lv. 14 llllllllll 
442 hit since 2005/02/21 19:18

Source code 만들기

편한대로 간단한 소스코드를 하나 작성한다. 이름을 hello.c라고 하겠다.

[cky@localhost hello]$ cat hello.c
#include <stdio.h>

int main()
{
        printf("Hello World!n");
        return 0;
}

Makefile.am 작성

Makefile.am은 Makefile.in을 만들기 위한 준비 파일이다. 다음과 같이 작성한다.
[cky@localhost hello]$ cat Makefile.am
bin_PROGRAMS=hello
hello_SOURCES=hello.c

bin_PROGRAMS은 프로그램이 설치될 디렉토리를 의미하고($PREFIX/bin으로 보통은 /usr/local/bin이 된다.)
hello_SOURCES는 hello란 프로그램에 대한 소스 파일들을 적어준다.
자세한 내용은 GNU automake document를 참조해야된다.

configure.ac 작성

autoscan명령을 통해서 configure.scan파일을 얻을 수 있다. 이 파일은 configure.ac 파일을 만드는 기초가 되는 파일이다.

[cky@localhost hello]$ autoscan
[cky@localhost hello]$ cat configure.scan
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.57)
AC_INIT(FULL-PACKAGE-NAME, VERSION, BUG-REPORT-ADDRESS)
AC_CONFIG_SRCDIR([hello.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])
AC_OUTPUT


편집기를 이용하여 다음과 같이 수정한다. 이 configure.scan은 완벽한것이 아니며, 항상 수정이 필요로 한 듯 하다.
수정이 끝났으면 configure.ac로 이름을 바꿔준다.

[cky@localhost hello]$ mv configure.scan configure.ac  
[cky@localhost hello]$ cat configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.57)
AC_INIT(hello, 0.1, secrice@gmail.com)
AM_INIT_AUTOMAKE(hello, 0.1)
AC_CONFIG_SRCDIR([hello.c])
AM_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([Makefile])
AC_OUTPUT


수정된 곳은 AC_INIT 매크로에서 프로그램 이름, 버전 정보 등이 수정하였고, AM_INIT_AUTOMAKE 매크로를 추가하였다. 또 AC_CONFIG_HEADER가 AM_CONFIG_HEADER로 변경하였다.

configure 만들기

다음과 같은 명령에 의해서 configure 스크립트를 얻는다.
[cky@localhost hello]$ aclocal
[cky@localhost hello]$ autoheader
[cky@localhost hello]$ autoconf
[cky@localhost hello]$ automake --add-missing
configure.ac: installing `./install-sh'
configure.ac: installing `./mkinstalldirs'
configure.ac: installing `./missing'
Makefile.am: installing `./INSTALL'
Makefile.am: required file `./NEWS' not found
Makefile.am: required file `./README' not found
Makefile.am: installing `./COPYING'
Makefile.am: required file `./AUTHORS' not found
Makefile.am: required file `./ChangeLog' not found
Makefile.am: installing `./depcomp'

메시지를 살펴보면, NEWS, README, AUTHORS, ChangeLog 파일이 필요하다고 나와있다. 파일의 목적은 파일 이름으로 알 수 있지만 내용은 없어도 된다. 일단 파일을 만들어두기만 하면 된다.
[cky@localhost hello]$ touch NEWS
[cky@localhost hello]$ touch README
[cky@localhost hello]$ touch AUTHORS
[cky@localhost hello]$ touch ChangeLog
[cky@localhost hello]$ automake --add-missing

build

이제 프로그램을 빌드해본다. 정상적으로 만들어졌다면 다음의 과정으로 컴파일이 될 것이다.
[cky@localhost hello]$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... gcc
checking for C compiler default output... a.out
checking whether the C compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands
[cky@localhost hello]$ make
make  all-am
make[1]: Entering directory `/home/cky/work/hello'
source='hello.c' object='hello.o' libtool=no
depfile='.deps/hello.Po' tmpdepfile='.deps/hello.TPo'
depmode=gcc3 /bin/sh ./depcomp
gcc -DHAVE_CONFIG_H -I. -I. -I.     -g -O2 -c `test -f 'hello.c' || echo './'`hello.c
gcc  -g -O2   -o hello  hello.o 
make[1]: Leaving directory `/home/cky/work/hello'
[cky@localhost hello]$ ls -lh hello
-rwxrwxr-x    1 cky      cky          6.6K Nov 14 20:49 hello
[cky@localhost hello]$ ./hello
Hello World!

distribution

이제 배포도 쉽게 할 수 있다. make dist 라고 치면 Packagename-version.tar.gz라는 파일을 만들어준다. 다른 사람들은 tar.gz 파일만 가져다가 configure, make, make install만 하면 쓸 수 있게 되는 것이다.
[cky@localhost hello]$ make dist
{ test ! -d hello-0.1 || { find hello-0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr hello-0.1; }; }
mkdir hello-0.1
find hello-0.1 -type d ! -perm -777 -exec chmod a+rwx {} ; -o
  ! -type d ! -perm -444 -links 1 -exec chmod a+r {} ; -o
  ! -type d ! -perm -400 -exec chmod a+r {} ; -o
  ! -type d ! -perm -444 -exec /bin/sh /home/cky/work/hello/install-sh -c -m a+r {} {} ;
|| chmod -R a+r hello-0.1
/bin/sh /home/cky/work/hello/missing --run tar chof - hello-0.1 | GZIP=--best gzip -c > hello-0.1.tar.gz
{ test ! -d hello-0.1 || { find hello-0.1 -type d ! -perm -200 -exec chmod u+w {} ';' && rm -fr hello-0.1; }; }
[cky@localhost hello]$ ls -lh hello-0.1.tar.gz
-rw-rw-r--    1 cky      cky           59K Nov 14 20:51 hello-0.1.tar.gz