autotools + with-mysql

書くと言いつつだいぶサボっていた autotools のメモ。

今回は、–with-mysql の付け方をメモ。

サンプルとして書いたコードが以下のもの。

#include <stdio.h>
#include <mysql.h>
int main()
{
    MYSQL     sql;
    MYSQL_RES   *pres;
    MYSQL_ROW   row;
   if( !mysql_init( &sql))
   {
       puts("init error.");
       return0;
   }
   if( !mysql_real_connect( &sql,"localhost","root","root","testdb",0,NULL,0))
   {
       puts("connect error.");
       return0;
   }
   if( mysql_query( &sql,"SELECT count(*) from tb_test"))
   {
       puts("query error.");
       return0;
   }
   if( !( pres = mysql_store_result( &sql)))
   {
       puts("store error.");
       return0;
   }
   if( !( row = mysql_fetch_row( pres)))
   {
       puts("fetch row error.");
       return0;
   }
   printf("count = %s\n", row[0]);
   return0;
}

単純に testdb にコネクトして tb_test の行をカウントして表示しているだけ。

通常、これを make する場合、-I/usr/local/mysql/include などと INCLUDE 先や LIBS 先を Makefileにハードコーディングする必要があります。

MySQL のインストール先が変わったりしたら、その度に vi で編集して。。。なんてメンドスギ!

そこで –with-mysql オプション。

./configure –with-mysql=/usr/local/mysql と指定すれば、自動でMakefileが記述されます。 MySQL のインストール先が違っても –with-mysql の指定先を変更して configure し直せばいいだけ! すばらすぃー。

以下、configure.in に追加したコード。

# Checks for MySQL
AC_MSG_CHECKING([for MySQL])
AC_ARG_WITH([mysql],
          [AS_HELP_STRING([--with-mysql=@<:@ARG@:>@],[path to'mysql_config' or path to mysql directory])],
          [with_mysql=$withval],
          [AC_MSG_RESULT([no])
            AC_MSG_ERROR([MySQL is required.])])
find_mysql="no"
if test $with_mysql ="no"; then
    AC_MSG_ERROR([MySQL is required.])
fi
if test -d $with_mysql; then
    for mysql_config in"${with_mysql}/mysql_config""${with_mysql}/bin/mysql_config"
    do
        if test -x ${mysql_config}; then
            MYSQL_INCLUDE=`${mysql_config} --include`
            MYSQL_LIBS=`${mysql_config} --libs`
            MYSQL_LIBS_R=`${mysql_config} --libs_r`
            find_mysql="yes"
            break1;
        fi
    done
elif test -x ${with_mysql}; then
    MYSQL_INCLUDE=`$with_mysql --include`
    MYSQL_LIBS=`$with_mysql --libs`
    MYSQL_LIBS_R=`$with_mysql --libs_r`
    find_mysql="yes"
fi
AC_MSG_RESULT([$find_mysql])
if test $find_mysql ="no"; then
    AC_MSG_ERROR(['mysql_config' was not found. :${with_mysql}])
fi
AC_SUBST( MYSQL_INCLUDE)
AC_SUBST( MYSQL_LIBS)
AC_SUBST( MYSQL_LIBS_R)

指定されたディレクトリ配下の mysql_config を探しにいくやら、 もしくは、直接 mysql_config を指定してもらう形式。 mysql_config とは、コンパイルオプションを取得するスクリプト。

ソースからインストールしたら、mysql/bin に入っています。 RPMなどバイナリの場合は、MySQL-devel パッケージ が必要らしい。

AC_MSG_CHECKING は、checking … って表示されるやつ。 AC_ARG_WITH が –with をつけてくれるマクロ。 第2引数は、ヘルプ。第3引数は、指定した場合。第4引数は、指定していない場合に入る。

今回は、–with-mysql 無指定の場合、 AC_MSG_ERROR マクロで configure を中止させています。 また、見つからなかった場合も中止させています。

見つかったら、AC_SUBST で外部でも使えるようにします。

最後に Makefile.am

INCLUDES = @MYSQL_INCLUDE@
LIBS = @MYSQL_LIBS_R@
bin_PROGRAMS = hello
hello_SOURCES = main.cpp

@…@ で囲まれた変数が、シェル変数に置き換わります。

これでバッチリ。

あとは、configure を作り直して、再度configure を行えば終了。

$ autoreconf -i
$ ./configure --with-mysql=/usr/local/mysql
$ make
$ ./hello
count = 3

実行時にエラーが出る場合、おそらくライブラリを認識していない為。

ldconfig などを使って認識させるか、もしくは以下のようにすれば OK かと。 (とはいえ、このコードもスクリプト化して autotools 化しないと –with-mysql の意味がない)

$ LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql ./hello

以上、メモでした。