How do I include mysql.h and my_global.h into the Makefile.am?

让人想犯罪 __ 提交于 2019-12-11 16:27:18

问题


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?


回答1:


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.




回答2:


I did something similar: in the configure.ac:

AC_DEFUN([CHECK_MYSQL_LIB],
[
AC_CHECK_PROGS(
    MYSQL_CONFIG,
    mysql_config
)

  #In case it fails to find pthread then exit configure 
  if test "x${MYSQL_CONFIG}" != xmysql_config; then
    echo "------------------------------------------"
    echo " The mysql library and header file is      "
    echo " required to build this project. Stopping "
    echo " Check 'config.log' for more information. "
    echo "------------------------------------------"
    (exit 1); exit 1;
  else
    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])
  fi
])

And in the Makefile.am:

db_CPPFLAGS=$(MYSQL_CONFIG_CPPFLAGS)
db_LDFLAGS=$(MYSQL_CONFIG_LIBS)

Note that is better to add in the LDFLAGS instead of LDADD.



来源:https://stackoverflow.com/questions/10799185/how-do-i-include-mysql-h-and-my-global-h-into-the-makefile-am

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!