みつきんのメモ

組み込みエンジニアです。Interface誌で「Yocto Projectではじめる 組み込みLinux開発入門」連載中

ものすごく久しぶりにAutotoolsを使ってみた

はじめに

表題のとおり。

プロジェクト作成

Autotoolsを使用したプロジェクトを作成する。

ディレクトリ構造

ディレクトリ構造は以下のようになる。

hello
├── Makefile.am
├── configure.ac
└── src
    ├── Makefile.am
    ├── hello.cpp
    ├── hello.h
    └── main.cpp

ソースコード

ベースとなるソースコードを作成する。

  • hello.h
  • hello.cpp
  • main.cpp

hello.h

とりあえずヘッダファイルがある状態を試したいので適当にクラスを定義する。

#ifndef HELLO_H
#define HELLO_H

#include <string>

class Hello {
    std::string to_say_;
public:
    explicit Hello(const std::string& to_say);
    void say() const;
};

#endif //HELLO_H

hello.cpp

クラスを定義したので実装する。

#include "hello.h"
#include <iostream>

Hello::Hello(const std::string& to_say) : to_say_(to_say) {
}

void Hello::say() const {
    std::cout << "Hello " << to_say_ << std::endl;
}

main.cpp

プログラムの本体。

#include "hello.h"

int main() {
    Hello("world").say();
    return 0;
}

src/Makefile.am

srcディレクトリ内のMakefile.am

bin_PROGRAMS = hello
hello_SOURCES = hello.cpp main.cpp

Makefile.am

トップディレクトリのMakefile.am サブディレクトリの定義のみ.

SUBDIRS = src

configure.ac

configure.acの雛形を作成する。

$ autoscan
$ mv ./configure.scan ./configure.ac

その後適宜修正を行う。

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

AC_PREREQ([2.71])
AC_INIT([hello], [0.0.1], [mickey.happygolucky@gmail.com])
AM_INIT_AUTOMAKE([foreign])
AC_CONFIG_SRCDIR([src/hello.h])
AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CXX
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

プロジェクトの生成

ケルトンが作成できたのでconfigure.acからconfiugreを生成する。

$ autoreconf -i

ビルド

ビルドを行う。

$ ./configure
$ make -j

動作確認

実行してみる。

$ src/hello
Hello world

まとめ

Autotoolsはかなり忘れていた。 C++もしばらく遠ざかっているので古臭いコードなのは勘弁。