It is often faster and simpler to use GNU autotools than to
write your own makefiles. In this section, we provide an
autoconf macro
WITH_MYSQL
that can be used to add a
--with-mysql
option to a configure file, and
that automatically sets the correct compiler and linker flags
for given MySQL installation.
All of the examples in this chapter include a common
mysql.m4
file defining
WITH_MYSQL
. A typical complete example
consists of the actual source file and the following helper
files:
acinclude
configure.in
Makefile.m4
automake also requires that you
provide README
, NEWS
,
AUTHORS
, and ChangeLog
files; however, these can be left empty.
To create all necessary build files, run the following:
aclocal
autoconf
automake -a -c
configure --with-mysql=/mysql/prefix/path
Normally, this needs to be done only once, after which make will accommodate any file changes.
Example 1-1: acinclude.m4.
m4_include([../mysql.m4])
Example 1-2: configure.in.
AC_INIT(example, 1.0)
AM_INIT_AUTOMAKE(example, 1.0)
WITH_MYSQL()
AC_OUTPUT(Makefile)
Example 1-3: Makefile.am.
bin_PROGRAMS = example
example_SOURCES = example.cc
Example 1-4: WITH_MYSQL source for inclusion in acinclude.m4.
dnl
dnl configure.in helper macros
dnl
AC_DEFUN([WITH_MYSQL], [
AC_MSG_CHECKING(for mysql_config executable)
AC_ARG_WITH(mysql, [ --with-mysql=PATH path to mysql_config binary or mysql prefix dir], [
if test -x $withval -a -f $withval
then
MYSQL_CONFIG=$withval
elif test -x $withval/bin/mysql_config -a -f $withval/bin/mysql_config
then
MYSQL_CONFIG=$withval/bin/mysql_config
fi
], [
if test -x /usr/local/mysql/bin/mysql_config -a -f /usr/local/mysql/bin/mysql_config
then
MYSQL_CONFIG=/usr/local/mysql/bin/mysql_config
elif test -x /usr/bin/mysql_config -a -f /usr/bin/mysql_config
then
MYSQL_CONFIG=/usr/bin/mysql_config
fi
])
if test "x$MYSQL_CONFIG" = "x"
then
AC_MSG_RESULT(not found)
exit 3
else
AC_PROG_CC
AC_PROG_CXX
# add regular MySQL C flags
ADDFLAGS=`$MYSQL_CONFIG --cflags`
# add NDB API specific C flags
IBASE=`$MYSQL_CONFIG --include`
ADDFLAGS="$ADDFLAGS $IBASE/storage/ndb"
ADDFLAGS="$ADDFLAGS $IBASE/storage/ndb/ndbapi"
ADDFLAGS="$ADDFLAGS $IBASE/storage/ndb/mgmapi"
CFLAGS="$CFLAGS $ADDFLAGS"
CXXFLAGS="$CXXFLAGS $ADDFLAGS"
LDFLAGS="$LDFLAGS "`$MYSQL_CONFIG --libs_r`" -lndbclient"
LDFLAGS="$LDFLAGS "`$MYSQL_CONFIG --libs_r`" -lndbclient"
AC_MSG_RESULT($MYSQL_CONFIG)
fi
])