コグノスケ


link 未来から過去へ表示(*)  link 過去から未来へ表示

link もっと前
2015年12月27日 >>> 2015年12月27日
link もっと後

2015年12月27日

GNU autotools入門 その2

その1その2

GNU autotoolsは簡単なんですが、使い始めるまではかなり難儀した記憶があります。例えば、下記のようにlibtest_new.soという名前の「動的ライブラリ」を作成するプロジェクトを作ったとします。

GNU autotoolsで動的ライブラリをビルドするプロジェクト
.
|-- Makefile.am
|-- configure.ac
`-- test
    |-- Makefile.am
    |-- common
    |   |-- Makefile.am
    |   `-- utils.c
    `-- test.c

----
configure.ac
----
AC_PREREQ(2.61)
AC_INIT([automake_test], [0.1])
AC_ARG_PROGRAM()
AC_CONFIG_SRCDIR([test/test.c])
AC_CONFIG_HEADER([config.h])
AC_CONFIG_AUX_DIR([conf]) ★ここで指定したディレクトリに注目★
AM_INIT_AUTOMAKE([foreign])
LT_INIT() ★これも注目★

AC_CONFIG_FILES([Makefile
	test/Makefile test/common/Makefile])

AC_OUTPUT()


----
test/Makefile.am
----
SUBDIRS = common

test_newdir = $(libdir)
test_new_LTLIBRARIES = libtest_new.la

libtest_new_la_SOURCES = test.c

libtest_new_la_LIBADD = common/libcommon.la


----
test/common/Makefile.am
----
noinst_LTLIBRARIES = libcommon.la

libcommon_la_SOURCES = utils.c

基本的にGNU autotoolsを使う時はautoreconfというコマンドを実行すれば、勝手に全ての関連ファイルが更新されます。

ただ残念なことに、初回だけはそうもいきません。

初回のautoreconfの結果
$ autoreconf
configure.ac:16: error: required file 'conf/compile' not found
configure.ac:16:   'automake --add-missing' can install 'compile'
configure.ac:16: error: required file 'conf/config.guess' not found
configure.ac:16:   'automake --add-missing' can install 'config.guess'
configure.ac:16: error: required file 'conf/config.sub' not found
configure.ac:16:   'automake --add-missing' can install 'config.sub'
configure.ac:15: error: required file 'conf/install-sh' not found
configure.ac:15:   'automake --add-missing' can install 'install-sh'
configure.ac:16: error: required file 'conf/ltmain.sh' not found
configure.ac:15: error: required file 'conf/missing' not found
configure.ac:15:   'automake --add-missing' can install 'missing'
test/Makefile.am: error: required file 'conf/depcomp' not found
test/Makefile.am:   'automake --add-missing' can install 'depcomp'
autoreconf2.50: automake failed with exit status: 1

このようにたくさん怒られます。何を怒っているのかというと、AC_CONFIG_AUX_DIR([conf]) で指定したディレクトリ(詳細は automakeのマニュアル参照)に、補助ツールが入ってないじゃないか!と怒っているのです。

エラーメッセージの最後に「'automake --add-missing' can install 'depcomp'」とあるように、優しいautotoolsさんは解決策も用意してくれています。これもやってみましょう。

add-missingを実行した結果
$ automake --add-missing
configure.ac:16: installing 'conf/compile'
configure.ac:16: installing 'conf/config.guess'
configure.ac:16: installing 'conf/config.sub'
configure.ac:15: installing 'conf/install-sh'
configure.ac:16: error: required file 'conf/ltmain.sh' not found
configure.ac:15: installing 'conf/missing'
test/Makefile.am: installing 'conf/depcomp'

まだ何か怒っています。ltmain.shが無いそうです。これはlibtoolというツールの一部です。

最初に述べたようにこのプロジェクトは「動的ライブラリ」を作るプロジェクトです。GNU autotoolsは動的ライブラリをビルドする際にlibtoolという別のツールを使っています。プロジェクトのconfigure.acにLT_INIT() と書いていましたよね。この行はlibtoolを使うことをautotoolsに伝えるため(詳細は libtoolのマニュアル参照)にあります。

逆に言えば、LT_INIT() が無いと動的ライブラリはビルドできません。そんなの知るかって?正直言って私もそう思いました、けど、今のところそういう作りなので我慢するしかありません…。

さてltmain.shが何者かもわかったところで、肝心のどこから持って来るか?ですが、こんなときってfindが便利ですよね?

ltmain.shを無理やり持ってくる
$ find /usr -name ltmain.sh
/usr/share/libtool/config/ltmain.sh

$ cp /usr/share/libtool/config/ltmain.sh conf/ltmain.sh

$ automake --add-missing

$ tree
.
|-- Makefile.am
|-- Makefile.in
|-- aclocal.m4
|-- conf
|   |-- compile -> /usr/share/automake-1.14/compile
|   |-- config.guess -> /usr/share/automake-1.14/config.guess
|   |-- config.sub -> /usr/share/automake-1.14/config.sub
|   |-- depcomp -> /usr/share/automake-1.14/depcomp
|   |-- install-sh -> /usr/share/automake-1.14/install-sh
|   |-- ltmain.sh
|   `-- missing -> /usr/share/automake-1.14/missing
|-- config.h.in
|-- configure
|-- configure.ac
`-- test
    |-- Makefile.am
    |-- Makefile.in
    |-- common
    |   |-- Makefile.am
    |   |-- Makefile.in
    |   `-- utils.c
    `-- test.c

以上のようにltmain.shを無理やり持って来たら、エラーも警告も出なくなりました。めでたしめでたし、です。

もし他の環境に持っていく場合、conf/missingなどのシンボリックリンクはリンク先に何もない可能性がある(※)ので、実体に置き換えておくと良いでしょう。

(※)autotoolsをインストールしていない環境もあれば、インストールされていたとしても違うディレクトリに置かれている可能性があります。

全てを台無しにする

既にautotoolsをお使いの方はもちろんのこと、autoreconfという見慣れぬコマンドに対して、すぐにヘルプを見た方はお気づきかと思いますが、今までの手順を一撃でやってくれるコマンドがあります。

その名もautoreconf --installです。早速やってみましょう。

今までの苦労は何だったのか
$ tree
.
|-- Makefile.am
|-- configure.ac
`-- test
    |-- Makefile.am
    |-- common
    |   |-- Makefile.am
    |   `-- utils.c
    `-- test.c

$ autoreconf --install
libtoolize: putting auxiliary files in AC_CONFIG_AUX_DIR, `conf'.
libtoolize: copying file `conf/ltmain.sh'
libtoolize: Consider adding `AC_CONFIG_MACRO_DIR([m4])' to configure.ac and
libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree.
libtoolize: Consider adding `-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
configure.ac:16: installing 'conf/compile'
configure.ac:16: installing 'conf/config.guess'
configure.ac:16: installing 'conf/config.sub'
configure.ac:15: installing 'conf/install-sh'
configure.ac:15: installing 'conf/missing'
test/Makefile.am: installing 'conf/depcomp'

$ tree
.
|-- Makefile.am
|-- Makefile.in
|-- aclocal.m4
|-- conf
|   |-- compile
|   |-- config.guess
|   |-- config.sub
|   |-- depcomp
|   |-- install-sh
|   |-- ltmain.sh
|   `-- missing
|-- config.h.in
|-- configure
|-- configure.ac
`-- test
    |-- Makefile.am
    |-- Makefile.in
    |-- common
    |   |-- Makefile.am
    |   |-- Makefile.in
    |   `-- utils.c
    `-- test.c

シンボリックリンクを実体に置き換えるところまで勝手にやってくれます。いやー、もう全部これだけで良いんじゃない?というくらい、素晴らしいです。

スクラッチビルドのススメ

GNU autotoolsを使う時は、常にautoreconf --install頼りでもそこそこ動くはずですが、いざautoreconfがエラーで止まった時は手も足も出なくなってしまいます。

私個人の悲しい経験から言って、この手の便利ツールって、中で何をやっているのかサッパリわからないので、トラブルが起きると非常に苦労します。

  • ツールを動かすのに最低限どんなファイルが必要か?
  • 無いときはどんなエラーが出るか?
  • エラーの解消には何をすれば良いか?

ツールのコードを読めとは言いません(私も読んだことありません)が、せめてこのくらいは調べた方が良いです。トラブルシュートの早さが断然違います。

これらを調べるには、マニュアルを当てずっぽうに読むより、スクラッチからプロジェクトを作ってみるのが一番効きます。既存プロジェクトのコピペでは得られなかった、新たな発見があると思いますよ。

編集者:すずき(2018/05/20 03:59)

コメント一覧

  • 名前さん(2018/05/11 12:40)
    助かりました。勉強になります。
  • すずきさん(2018/05/12 21:45)
    お役に立てて何よりです。
open/close この記事にコメントする



link もっと前
2015年12月27日 >>> 2015年12月27日
link もっと後

管理用メニュー

link 記事を新規作成

<2015>
<<<12>>>
--12345
6789101112
13141516171819
20212223242526
2728293031--

最近のコメント5件

  • link 21年3月13日
    すずきさん (03/05 15:13)
    「あー、このプログラムがまずいんですね。ご...」
  • link 21年3月13日
    emkさん (03/05 12:44)
    「キャストでvolatileを外してアクセ...」
  • link 24年1月24日
    すずきさん (02/19 18:37)
    「簡単にできる方法はPowerShellの...」
  • link 24年1月24日
    KKKさん (02/19 02:30)
    「追伸です。\nネットで調べたらマイクロソ...」
  • link 24年1月24日
    KKKさん (02/19 02:25)
    「私もエラーで困ってます\n手動での回復パ...」

最近の記事3件

  • link 24年3月25日
    すずき (03/26 03:20)
    「[Might and Magic Book One TASのその後] 目次: Might and Magicファミコン版以前(...」
  • link 21年10月4日
    すずき (03/26 03:14)
    「[Might and Magicファミコン版 - まとめリンク] 目次: Might and Magicファミコン版TASに挑...」
  • link 24年3月19日
    すずき (03/20 02:52)
    「[モジュラージャックの規格] 古くは電話線で、今だとEthernetで良く見かけるモジュラージャックというコネクタとレセプタク...」
link もっとみる

こんてんつ

open/close wiki
open/close Linux JM
open/close Java API

過去の日記

open/close 2002年
open/close 2003年
open/close 2004年
open/close 2005年
open/close 2006年
open/close 2007年
open/close 2008年
open/close 2009年
open/close 2010年
open/close 2011年
open/close 2012年
open/close 2013年
open/close 2014年
open/close 2015年
open/close 2016年
open/close 2017年
open/close 2018年
open/close 2019年
open/close 2020年
open/close 2021年
open/close 2022年
open/close 2023年
open/close 2024年
open/close 過去日記について

その他の情報

open/close アクセス統計
open/close サーバ一覧
open/close サイトの情報

合計:  counter total
本日:  counter today

link About www.katsuster.net
RDFファイル RSS 1.0

最終更新: 03/26 03:20