I'm trying to create a C program that can communicate with MySQL database via these two header files:
mysql.h
my_global.h
MySQL comes with mysql_config script that you can execute to find out where the include files and library files reside in the system. I was wondering how would you define it in the Makefile.am?
I currently have the following:
bin_PROGRAMS = db
db_SOURCES = db.c db.h
db_CFLAGS = -Wall `mysql_config --cflags --libs`
Is this the correct way?
解决方案
I would search for mysql_config in configure.ac using the AX_WITH_PROG macro:
AX_WITH_PROG([MYSQL_CONFIG], [mysql_config], [AC_MSG_ERROR(mysql_config is required to build)])
so your users will be able to point the MYSQL_CONFIG environment variable at the program should it be installed in an unexpected location. And if the users haven't installed it, they will get a nice error message which alerts them to that fact before attempting to build.
I'd probably set up the cflags, cppflags, and libs in configure.ac as well, since they shouldn't change after configure is run:
MYSQL_CONFIG_CFLAGS=`$MYSQL_CONFIG --cflags`
MYSQL_CONFIG_CPPFLAGS=`$MYSQL_CONFIG --include`
MYSQL_CONFIG_LIBS=`$MYSQL_CONFIG --libs`
AC_SUBST([MYSQL_CONFIG_CFLAGS])
AC_SUBST([MYSQL_CONFIG_CPPFLAGS])
AC_SUBST([MYSQL_CONFIG_LIBS])
and put them in place in Makefile.am
db_CFLAGS = -Wall $(MYSQL_CONFIG_CFLAGS)
db_CPPFLAGS=$(MYSQL_CONFIG_CPPFLAGS)
db_LDADD=$(MYSQL_CONFIG_LIBS)
If all you need is the header files, you probably won't need to set up the cflags variable.