ラベル Makefile の投稿を表示しています。 すべての投稿を表示
ラベル Makefile の投稿を表示しています。 すべての投稿を表示

2013年2月2日土曜日

[Makefile]Automake でサブディレクトリ内もコンパイルする

次のようなディレクトリ構成のソースをコンパイルする場合
hello
└─src
    │  hello.c
    │  hello.h
    │  main.c
    │  Makefile.am
    │
    └─extfunc
            extfunc.c
            extfunc.h
            Makefile.am
main.c から extfunc.c 内の関数をコールするようにしている。
#include "hello.h"
#include "extfunc/extfunc.h"

int main(void)
{
 say_hello();
 test();
 extfunc();

 return 0;
}
Example 1. extfunc.c
#include "extfunc.h"

int extfunc(void)
{
 printf("extfunc is called!!\n");

 return 0;
}
Example 2. extfunc.h
#ifndef __EXTFUNC_H__
#define __EXTFUNC_H__

extern int extfunc(void);

#endif /* __EXTFUNC_H__ */
automake では直接サブディレクトリのソースを取り込んでコンパイルすることができないようなので、extfunc ディレクトリ内のオブジェクトはライブラリにして最後にリンクさせるようにする。

Example 3. src/extfunc/Makefile.am
noinst_LIBRARIES = libextfunc.a
libextfunc_a_SOURCES = extfunc.c
Example 4. src/Makefile.am
bin_PROGRAMS = hello
hello_SOURCES = hello.c hello.h main.c
hello_LDADD = extfunc/libextfunc.a
SUBDIRS = extfunc
LDADD を使用するために configure.ac に AC_PROG_RANLIB を追加する必要がある。そのため automake を行うスクリプトも修正する。
#!/bin/bash -v
autoscan
sed -e "s/FULL-PACKAGE-NAME/hello/g" -e "s/VERSION/1.0/g" -e "s/BUG-REPORT-ADDRESS/hoge@hogehoge/g" -e "6 i AM_INIT_AUTOMAKE([foreign])" -e "$ i AC_PROG_RANLIB" configure.scan > configure.ac
autoheader
aclocal
automake --add-missing --copy
autoconf
./configure
sed -e "$ i AC_PROG_RANLIB" configure.scan > configure.ac
で configure.scan の最後の行に AC_PROG_RANLIB を追加している。

[Makefile]automake を使用して Makefile を作成するまでのスクリプト

autoscan, autoheader, aclocal などのコマンドを順番に実行するのは面倒なのでスクリプトにまとめてみた。
#!/bin/bash -v
autoscan
sed -e "s/FULL-PACKAGE-NAME/hello/g" -e "s/VERSION/1.0/g" -e "s/BUG-REPORT-ADDRESS/hoge@hogehoge/g" -e "6 i AM_INIT_AUTOMAKE([foreign])" configure.scan > configure.ac
autoheader
aclocal
automake --add-missing --copy
autoconf
./configure
#!/bin/bash -v
としておくことで実行コマンドを表示させることができる。
configure.scan の中身を書き換えて configure.ac を生成するところは sed コマンドで置換と挿入を行う。

[Makefile]automakeを使用してMakefileを作成する手順

以下のディレクトリ構成でautomakeを実施する手順を記述する。
hello
│
└─src
        hello.c
        hello.h
        main.c
  1. hello/Makefile.am を作成
  2. SUBDIRS = src
    
  3. hello/src/Makefile.am を作成
    bin_PROGRAMS = hello
    hello_SOURCES = hello.c hello.h main.c
    
    この時点でのディレクトリ構成は以下の通り(*は新規追加されたファイル)
    hello
    │  Makefile.am (*)
    │
    └─src
            hello.c
            hello.h
            main.c
            Makefile.am (*)
    
  4. hello直下でautoscanを実行しconfigure.scanを作成する
  5. configure.scanをconfigure.acに改名して以下の箇所を修正
    1. AC_INITの中身を修正
      AC_INIT([hello, 1.0], [hoge@hogehoge])
      
    2. AC_INITの次の行に以下を追加
      AM_INIT_AUTOMAKE([foreign])
      
    この時点でのディレクトリ構成は以下の通り(*は新規追加されたファイル)
    hello
    │  autoscan-2.63.log (*)
    │  configure.ac (*)
    │  Makefile.am
    │
    └─src
            hello.c
            hello.h
            main.c
            Makefile.am
    
  6. autoheader 実行
    この時点でのディレクトリ構成は以下の通り(*は新規追加されたファイル)
    hello
    │  autoscan-2.63.log
    │  config.h.in (*)
    │  configure.ac
    │  Makefile.am
    │
    ├─autom4te.cache (*)
    │      output.0 (*)
    │      requests (*)
    │      traces.0 (*)
    │
    └─src
            hello.c
            hello.h
            main.c
            Makefile.am
    
  7. aclocalを実行
    この時点でのディレクトリ構成は以下の通り(*は新規追加されたファイル)
    hello
    │  aclocal.m4 (*)
    │  autoscan-2.63.log
    │  config.h.in
    │  configure.ac
    │  Makefile.am
    │
    ├─autom4te.cache
    │      output.0
    │      output.1 (*)
    │      requests
    │      traces.0
    │      traces.1 (*)
    │
    └─src
            hello.c
            hello.h
            main.c
            Makefile.am
    
  8. automake ‑‑add-missing ‑‑copyを実行
    この時点でのディレクトリ構成は以下の通り(*は新規追加されたファイル)
    hello
    │  aclocal.m4
    │  autoscan-2.63.log
    │  config.h.in
    │  configure.ac
    │  depcomp (*)
    │  install-sh (*)
    │  Makefile.am
    │  Makefile.in (*)
    │  missing (*)
    │
    ├─autom4te.cache
    │      output.0
    │      output.1
    │      output.2 (*)
    │      requests
    │      traces.0
    │      traces.1
    │      traces.2 (*)
    │
    └─src
            hello.c
            hello.h
            main.c
            Makefile.am
            Makefile.in (*)
    
  9. autoconfを実行
    この時点でのディレクトリ構成は以下の通り(*は新規追加されたファイル)
    hello
    │  aclocal.m4
    │  autoscan-2.63.log
    │  config.h.in
    │  configure (*)
    │  configure.ac
    │  depcomp
    │  install-sh
    │  Makefile.am
    │  Makefile.in
    │  missing
    │
    ├─autom4te.cache
    │      output.0
    │      output.1
    │      output.2
    │      requests
    │      traces.0
    │      traces.1
    │      traces.2
    │
    └─src
            hello.c
            hello.h
            main.c
            Makefile.am
            Makefile.in
    
  10. configureを実行
    この時点でのディレクトリ構成は以下の通り(*は新規追加されたファイル)
    hello
    │  aclocal.m4
    │  autoscan-2.63.log
    │  config.h (*)
    │  config.h.in
    │  config.log (*)
    │  config.status (*)
    │  configure
    │  configure.ac
    │  depcomp
    │  install-sh
    │  Makefile (*)
    │  Makefile.am
    │  Makefile.in
    │  missing
    │  stamp-h1 (*)
    │
    ├─autom4te.cache
    │      output.0
    │      output.1
    │      output.2
    │      requests
    │      traces.0
    │      traces.1
    │      traces.2
    │
    └─src
        │  hello.c
        │  hello.h
        │  main.c
        │  Makefile (*)
        │  Makefile.am
        │  Makefile.in
        │
        └─.deps (*)
                hello.Po (*)
                main.Po (*)
    
  11. makeを実行
    この時点でのディレクトリ構成は以下の通り(*は新規追加されたファイル)
    hello
    │  aclocal.m4
    │  autoscan-2.63.log
    │  config.h
    │  config.h.in
    │  config.h.in~ (*)
    │  config.log
    │  config.status
    │  configure
    │  configure.ac
    │  depcomp
    │  install-sh
    │  Makefile
    │  Makefile.am
    │  Makefile.in
    │  missing
    │  stamp-h1
    │
    ├─autom4te.cache
    │      output.0
    │      output.1
    │      output.2
    │      requests
    │      traces.0
    │      traces.1
    │      traces.2
    │
    └─src
        │  hello.c
        │  hello.exe (*)
        │  hello.h
        │  hello.o (*)
        │  main.c
        │  main.o (*)
        │  Makefile
        │  Makefile.am
        │  Makefile.in
        │
        └─.deps
                hello.Po
                main.Po