본문 바로가기

C

C언어 개발 환경 구축 (2) - autotools

개요

C언어로 작성된 파일을 컴파일 하기위해서는 여러가지 방법이 있다. 최근에는 autotools의 단점을 보완 또는 대체 하기위한 많은 빌더들이 난립하고 있는 상황이다. 하지만 autotools는 이식성 높은 프로젝트 빌더 툴로 예전부터 최근까지 또는 앞으로도 많이 사용될 빌더로 반드시 학습해야 한다.

 

프로젝트 구성

  • 프로젝트: amhello
  • 디렉토리 구성:
amhello
└── src
    └── main.c
#include <stdio.h>

int main(int argc, char *argv[])
{
       printf("Hello autotools\n");

       return 0;
}

 

Root Makefile.am 파일작성

autotools는 가장 상위 디렉토리의 Makefile.am을 참조하여 SUBDIRS를 참조하여 진행한다.

amhello
├── Makefile.am <-- 파일 작성
└── src
    └── main.c
SUBDIRS = src

SUBDIRS의 Makefile.am 파일 작성

amhello
├── Makefile.am
└── src
    ├── Makefile.am <-- 파일 작성
    └── main.c
bin_PROGRAMS = amhello

amhello_SOURCES = main.c
  • bin_PROGRAMS: 컴파일할 프로그램 이름
  • {prog}_SOURCES: 컴파일에 필요한 소스 파일

 

Autoscan - configure.scan 템플릿을 생성

ubuntu@autotools:~/Workspaces/amhello$ autoscan

root 디렉토리에서 autoscan 명령을 수행하면 디렉토리를 검색하여 configure.scan이라는 템플릿 파일이 생성된다.

ubuntu@autotools:~/Workspaces/amhello$ mv configure.scan configure.ac

configure.scan 파일을 configure.ac 파일로  이름을 변경하고 내용을 수정한다.

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([amhello], [1.0], [onurmark1@gmail.com]) <- 패키지이름, 버전, 메일을 수정
AC_CONFIG_SRCDIR([src/main.c])
AC_CONFIG_HEADERS([config.h])

AM_INIT_AUTOMAKE([foreign -Wall -Werror])        <- automake를 사용할 수 있도록 추가

# 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
                 src/Makefile])
AC_OUTPUT

 

Autoconf

ubuntu@autotools:~/Workspaces/amhello$ autoreconf -i

autoreconf 명령은 autoconf에 포함된 파일 중하나로 configure.ac 파일을 입력으로 사용해 configure에 필요한 파일들을 생성해준다. -I 옵션은 보조적인 파일들을 생성한다.

 

amhello
├── COPYING
├── INSTALL
├── Makefile.am
├── Makefile.in
├── aclocal.m4
├── autom4te.cache
│   ├── output.0
│   ├── output.1
│   ├── requests
│   ├── traces.0
│   └── traces.1
├── autoscan.log
├── compile
├── config.h.in
├── config.h.in~
├── configure
├── configure.ac
├── configure.scan
├── depcomp
├── install-sh
├── missing
└── src
    ├── Makefile.am
    ├── Makefile.in
    └── main.c

해당 명령을 수행하면 패키지 및 빌더가 완성되었다. 

 

컴파일 및 실행하기

ubuntu@autotools:~/Workspaces/amhello$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
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 ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: executing depfiles commands
ubuntu@autotools:~/Workspaces/amhello$ make
make  all-recursive
make[1]: Entering directory '/home/ubuntu/Workspaces/amhello'
Making all in src
make[2]: Entering directory '/home/ubuntu/Workspaces/amhello/src'
gcc -DHAVE_CONFIG_H -I. -I..     -g -O2 -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.c
mv -f .deps/main.Tpo .deps/main.Po
gcc  -g -O2   -o amhello main.o
make[2]: Leaving directory '/home/ubuntu/Workspaces/amhello/src'
make[2]: Entering directory '/home/ubuntu/Workspaces/amhello'
make[2]: Leaving directory '/home/ubuntu/Workspaces/amhello'
make[1]: Leaving directory '/home/ubuntu/Workspaces/amhello'
ubuntu@autotools:~/Workspaces/amhello$ ./src/amhello
Hello autotools

지금까지 간단한 소스하나를 컴파일 하기 위해서 언뜻 복잡해 보이는 작업을 수행해주었다. 파일 하나를 컴파일 하기 위해 너무 많은 노력이 든것 같지만 보통 프로젝트하나에 수많은 소스 파일이 포함된다. 이때 사용자가 직접 빌드하는 것은 불가능하다. autotools 를 이용하면 복잡한 프로젝트도 간단히(?) 빌드 환경을 구성할 수 있다. autotools는 이식성이 높은 패키지로 크로스 컴파일이나 호환성 높은 이식성을 자랑한다. 수많은 프로젝트가 autotools로 구성되어 있으며 널리 사용되는 패키지 이므로 반드시 숙달하는 것이 필요하다.