보통 GNU Free Software 소스를 받아서 컴파일할때 제일먼저 하는일이 ./configure 를 하게됩니다. 실행되면 필요한 프로그램 체크와 Makefile 생성을 자동으로 완료합니다. GNU autotools를 이용하면 이러한 이식성 높은 빌드를 가지는 Project를 구현할수 있습니다.
autotools는 autoconf, automake, libtools, gettext가 포함되어있습니다. 이중에 autoconf, automake를 이용하여 예제 Project를 진행해 보겠습니다.
$ cat hello.c
$ cat lib/Makefile.am
참고 목록
공부한지 얼마 되지 않아 기록을 남겨 놓을 목적으로 글을 쓰고 있습니다. 잘못된점이 있다면 알려주시면 바로 반영토록 하겠습니다.
환경
OS : 우분투 10.10
Autoconf : autoconf (GNU Autoconf) 2.67
Automake : automake (GNU automake) 1.9.6
Autoconf : autoconf (GNU Autoconf) 2.67
Automake : automake (GNU automake) 1.9.6
예제 Project로 src와 lib을 가지는 구조로 만들어 볼 예정입니다. lib, src 디렉토리를 만들어보겠습니다.
$ mkidr lib src
$ tree
.
|-- lib
`-- src
이제 간단한 샘플 프로그램을 작성하도록 하겠습니다.
간단하게 Hello world 를 출력하는 함수를 가지는 라이브러리를 작성하게 됩니다.
library를 만들기위해 lib/아래에 hello.c, hello.h 를 작성합니다.
$ cat hello.h
#ifndef __HELLO_H__
#define __HELLO_H__
int hello( char *szString );
#endif /* __HELLO_H__ */
$ cat hello.c
#include <stdio.h>
#include "hello.h"
int hello( char *szString )
{
printf( "Hello world. %s\n", szString );
return 0;
}
library를 사용하는 프로그램을 작성합니다. src/아래에 main.c를 작성합니다.
$ cat main.c
#include <stdio.h>
#include <config.h>
#include <hello.h>
int main( )
{
printf( "hello main\n" );
hello( PACKAGE_STRING );
}
이제 샘플 프로그램 준비는 완료 됐으니 이제 autoconf와 automake를 이용해보겠습니다.
autoconf가 바라보는 설정 파일은 configure.ac 파일입니다. 이 파일의 기초를 만들기위한 autoscan이 있습니다.
$ autoscan
$ ls
autoscan.log configure.scan lib src
$ cat configure.scan
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.67])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([lib/hello.c])
AC_CONFIG_HEADERS([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_OUTPUT
configure.scan에서 아래내용을 수정후 파일명을 configure.ac로 변경합니다. 아래처럼 수정합니다.
$ cat configure.ac
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.67])
AC_INIT([ate_hello], [1.0], [bugreport@address]) #autoconf 초기화
AC_CONFIG_SRCDIR([lib/hello.c])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CC #C compiler 체크
AC_PROG_RANLIB #ranlib 체크
AM_INIT_AUTOMAKE( [foreign -Wall -Werror] ) #automake 초기화
AC_CONFIG_FILES( [Makefile lib/Makefile src/Makefile] ) #Makefile 선언(해당 위치에 Makefile.am이 있어야함)
# Checks for libraries.
# Checks for header files.
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
AC_OUTPUT
지금까지의 설정 작업은 autoconf를 위한 작업이었습니다. project build를 위해서는 automake를 위한 작업이 필요합니다.
automake가 바라보는 파일은 Makefile.am 입니다. automake는 Makefile.am을 Makefile로 변환하는 작업을 하게 됩니다.
./Makefile.am, lib/Makefile.am, src/Makefile.am 파일을 만들어야 됩니다.
$ cat Makefile.am
SUBDIRS = lib src
$ cat lib/Makefile.am
lib_LIBRARIES = libhello.a
libhello_a_SOURCES = hello.c hello.h
$ cat src/Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = main.c
hello_CFLAGS = -I$(srcdir)/../lib/ #AM_CFLAGS 사용할경우 전체 program에 적용
hello_LDADD = -L$(srcdir)/../lib/ -lhello #LDADD 사용할경우 전체 program에 적용
이제 autoreconf를 실행해줘야합니다. (autoreconf --install 혹은 autoreconf -i 로 실행해야합니다.)
$ autoreconf --install
configure.ac: installing `./install-sh'
configure.ac: installing `./missing'
lib/Makefile.am: installing `./depcomp'
src/Makefile.am: installing `./compile'
이제 configure를 실행시키고 make를 수행 하면 되겠습니다.
$ ./configure
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 for ranlib... ranlib
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
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 lib/Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
$ make
make all-recursive
make[1]: Entering directory `/home/sekim/drill/autotools'
Making all in lib
make[2]: Entering directory `/home/sekim/drill/autotools/lib'
if gcc -DHAVE_CONFIG_H -I. -I. -I.. -g -O2 -MT hello.o -MD -MP -MF ".deps/hello.Tpo" -c -o hello.o hello.c; \
then mv -f ".deps/hello.Tpo" ".deps/hello.Po"; else rm -f ".deps/hello.Tpo"; exit 1; fi
rm -f libhello.a
ar cru libhello.a hello.o
ranlib libhello.a
make[2]: Leaving directory `/home/sekim/drill/autotools/lib'
Making all in src
make[2]: Entering directory `/home/sekim/drill/autotools/src'
if gcc -DHAVE_CONFIG_H -I. -I. -I.. -I./../lib/ -g -O2 -MT hello-main.o -MD -MP -MF ".deps/hello-main.Tpo" -c -o hello-main.o `test -f 'main.c' || echo './'`main.c; \
then mv -f ".deps/hello-main.Tpo" ".deps/hello-main.Po"; else rm -f ".deps/hello-main.Tpo"; exit 1; fi
gcc -g -O2 -o hello hello-main.o -L./../lib/ -lhello
make[2]: Leaving directory `/home/sekim/drill/autotools/src'
make[2]: Entering directory `/home/sekim/drill/autotools'
make[2]: Leaving directory `/home/sekim/drill/autotools'
make[1]: Leaving directory `/home/sekim/drill/autotools'
이제 프로그램을 실행 시켜봅니다.
$ src/hello
hello main
Hello world. ate_hello 1.0
'프로그래밍 > general programming' 카테고리의 다른 글
우분투에 아나콘다 및 텐서플로우, 파이참 설치 하기 (0) | 2017.08.11 |
---|---|
사이보스 플러스 MFC 프로그램 작성 따라 하기 2/2 (Visual Studio 샘플프로그램) (0) | 2017.05.05 |
사이보스 플러스 MFC 프로그램 작성 따라 하기 1/2 (CYBOS plus 설치) (0) | 2017.05.05 |
[GNU autotools] libtool을 이용한 shared library 만들기 (0) | 2011.03.15 |