目次

Autoconf - configure スクリプトの生成

Autoconf は、さまざまなシステムに適応するようにソースコードパッケージを自動的に設定するシェルスクリプトを作成するためのツールです。
Autoconf によって生成された設定スクリプトは、実行時に Autoconf から独立しているため、ユーザーは Autoconf をインストールしている必要はありません。

各種コマンドは以下のパッケージに含まれる。

$ sudo dnf install automake

ビルドまでの流れ

手順 説明 フロー図
STEP1: Makefile.am の作成
$ touch INSTALL NEWS README LICENSE AUTHORS ChangeLog
$ vi Makefile.am
$ vi src/Makefile.am
Autoconf Workflow
STEP2: autoscan の実行
$ autoscan

カレントディレクトリやサブディレクトリの内容を元に configure.scan が生成される。

$ mv configure.scan configure.ac

configure.scan を configure.ac にリネーム。

STEP3: configure.ac の修正
$ vi configure.ac

configure.ac の内容を修正する。

STEP4: autoheader の実行
$ autoheader

configure.ac の内容を元に config.h.in が生成される。

STEP5: aclocal の実行
$ aclocal

configure.ac の内容を元に aclocal.m4 が生成される。

STEP6: autoconf の実行
$ autoconf

configure.ac と aclocal.m4 の内容を元に configure が生成される。

STEP7: automake の実行
$ automake --add-missing

configure.ac の内容を元に Makefile.in が生成される。

STEP8: ./configure; make の実行
$ ./configure; make

configure によって環境に応じた Makefile が生成されて、make でビルドが開始される。

※開発者ではない一般ユーザーは、STEP8 で ./configure; make のみでビルドを行う。

ディレクトリ構成

以下の構造を想定している。

autoconf_sample  (directory)
   +-AUTHORS     (file)
   +-ChangeLog   (file)
   +-INSTALL     (file)
   +-LICENSE     (file)
   +-Makefile.am (file)
   +-NEWS        (file)
   +-README      (file)
   +-configure.ac (file)
   +-src         (directory)
      +hello.cpp (file)

C++ サンプルの準備

ここでは wxGTK アプリの hello.cpp をサンプルとする。

src/hello.cpp
#include <vector>
#include <iterator>
#include <iostream>
#include <wx/wx.h>
#include <stdbool.h>
#include "config.h"
 
using namespace std;
 
// 定番のこんにちは世界
class HelloApp : public wxApp
{
public:
    bool OnInit();
};
 
IMPLEMENT_APP(HelloApp)
 
bool HelloApp::OnInit()
{
    std::vector<int> intVec; // intの vector を宣言
 
    intVec.push_back(10);   // いくつかの値を追加
    intVec.push_back(20);
    intVec.push_back(30);
 
    std::copy(intVec.begin(), intVec.end(), std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
 
    //ここから始まる
    wxFrame *frame = new wxFrame((wxFrame*)NULL, -1, "Hello World !!");
    frame->Show(true);      //フレームの表示
    SetTopWindow(frame);    //トップウィンドウに設定
    return true;
}

STEP1: Makefile.am の作成

空ファイルでも構わないので、INSTALL、NEWS、README、LICENSE、AUTHORS、ChangeLog のファイルを準備する。

$ touch AUTHORS ChangeLog INSTALL LICENSE NEWS README

Makefile.am を作成する。

$ vi Makefile.am
Makefile.am
# 作成する実行ファイル名
bin_PROGRAMS = hello
# 実行ファイルのソースコード
hello_SOURCES = src/hello.cpp
 
# C++コンパイラオプション
test_CXXFLAGS = -g

STEP2: autoscan の実行

ソースコードと Makefile.am が置かれたディレクトリで autoscan を実行する。

$ autoscan

autoscan はソースコードツリーを調べて、configure.scan という Autoconf の設定ファイルの雛形を生成する。

configure.scan
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
 
AC_PREREQ([2.69])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_CONFIG_SRCDIR([src/hello.cpp])
AC_CONFIG_HEADERS([config.h])
 
# Checks for programs.
AC_PROG_CC
AC_PROG_CXX
 
# Checks for libraries.
 
# Checks for header files.
 
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
 
# Checks for library functions.
 
AC_CONFIG_FILES([Makefile
                 src/Makefile])
AC_OUTPUT

configure.scan を configure.ac にリネームする。

$ mv configure.scan configure.ac

STEP3: configure.ac の修正

$ vi configure.ac
configure.ac
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
 
AC_PREREQ([2.69])
AC_INIT([hello],[1.0],[tomoyan@tomoyan.net])
AC_CONFIG_SRCDIR([src/hello.cpp])
AC_CONFIG_HEADERS([config.h])
 
AM_INIT_AUTOMAKE([1.16 subdir-objects foreign])
 
# Checks for programs.
AC_PROG_CC
AC_PROG_CXX
 
# Checks for libraries.
AM_OPTIONS_WXCONFIG
reqwx=3.0.4
AM_PATH_WXCONFIG($reqwx, wxWin=1)
if test "$wxWin" != 1; then
    AC_MSG_ERROR([
        wxWidgets must be installed on your system.
 
        Please check that wx-config is in path, the directory
        where wxWidgets libraries are installed (returned by
        'wx-config --libs' or 'wx-config --static --libs' command)
        is in LD_LIBRARY_PATH or equivalent variable and
        wxWidgets version is $reqwx or above.
        ])
fi
 
CPPFLAGS="$CPPFLAGS $WX_CPPFLAGS"
CXXFLAGS="$CXXFLAGS $WX_CXXFLAGS_ONLY"
CFLAGS="$CFLAGS $WX_CFLAGS_ONLY"
LIBS="$LIBS $WX_LIBS"
 
# Checks for header files.
 
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
 
# Checks for library functions.
 
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

STEP4: autoheader の実行

configure.ac の内容を元に config.h.in が生成される。

$ autoheader

STEP5: aclocal の実行

configure.ac の内容を元に aclocal.m4 が生成される。

$ aclocal

STEP6: autoconf の実行

configure.ac と aclocal.m4 の内容を元に configure が生成される。

$ autoconf

STEP7: automake の実行

configure.ac の内容を元に Makefile.in と src/Makefile.in が生成される。

$ automake --add-missing
configure.ac:12: installing './compile'
configure.ac:9: installing './install-sh'
configure.ac:9: installing './missing'
Makefile.am: installing './depcomp'

STEP8: ./configure; make の実行

configure によって環境に応じた Makefile が生成されて、make でビルドが開始される。

$ ./configure; make
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 for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking dependency style of g++... gcc3
checking for wx-config... /usr/bin/wx-config
checking for wxWidgets version >= 3.0.4... yes (version 3.0.4)
checking for wxWidgets static library... no
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for stdbool.h that conforms to C99... yes
checking for _Bool... yes
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands
make  all-am
make[1]: ディレクトリ '/home/tomoyan/my_projects/autoconf_sample' に入ります
depbase=`echo src/hello.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
g++ -DHAVE_CONFIG_H -I.   -I/usr/lib64/wx/include/gtk3-unicode-3.0 -I/usr/include/wx-3.0 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__  -g -O2  -MT src/hello.o -MD -MP -MF $depbase.Tpo -c -o src/hello.o src/hello.cpp &&\
mv -f $depbase.Tpo $depbase.Po
g++  -g -O2    -o hello src/hello.o  -pthread   -lwx_gtk3u_xrc-3.0 -lwx_gtk3u_webview-3.0 -lwx_gtk3u_html-3.0 -lwx_gtk3u_qa-3.0 -lwx_gtk3u_adv-3.0 -lwx_gtk3u_core-3.0 -lwx_baseu_xml-3.0 -lwx_baseu_net-3.0 -lwx_baseu-3.0 
make[1]: ディレクトリ '/home/tomoyan/my_projects/autoconf_sample' から出ます

hello の実行

$ ./hello
10 20 30

参考文献

Autoconf
Autoconf: Autoconf-2.59(日本語訳)
GNU Autoconf, Automake and Libtool
Autotoolsを使ってプロジェクトのMakefileを生成する