autoconf で ./configure --enable-xxxx を使う方法

よく ./configure 時に --enable-xxxx といった特定の機能を有効にするかどうかのオプションをつけますが、これを実現するための方法のまとめです。

今回は --enable-profiler という名前でオプションを作ります。

configure.ac

configure.ac に以下のように書きます。

AC_ARG_ENABLE(profiler, [  --enable-profiler trun on profiler [default no]],,enable_profiler=no)
AC_MSG_CHECKING(whether to enable profiler)
if test x$enable_profiler = xyes; then
  AC_MSG_RESULT(yes)
  AC_DEFINE(ENABLE_PROFILER, , enable -p option for profiling)
else
  AC_MSG_RESULT(no)
fi

面倒ですがコピペして必要なところを書き換えれば大丈夫です。
ネット上で検索していると

AC_DEFINE(ENABLE_PROFILER, , enable -p option for profiling)

の部分を

AC_DEFINE(ENABLE_PROFILER)

だけで済ませている例が良く見つかりますが動かないので注意してください。

autoheader

autoheader を実行してください。
そうすると config.h.in が書き換えられて ENABLE_PROFILER という定義の部分が付け加えられます。

./configure

./configure --enable-profiler してみます。

checking whether to enable profiler... yes

うまくいっていますね。

config.h

./configure --enable-profiler すると config.h に

/* enable -p option for profiling */
#define ENABLE_PROFILER 

と #define されているはずです。
あとはこの config.h を #include してやれば良いだけです。


意外と簡単でした。