This article mainly introduces how to use Autoconf to check MySQL software packages. it mainly uses the compiled ax_lib_mysql script for operations. if you need a friend, you can refer to it in your program (or project, if you need to check whether MySQL client-related library files exist in the current environment during the compilation phase, you can use Autoconf to help you complete this work, which is lightweight, elegant, and painless. Read this article to learn how to use GNU Autoconf.
1. objectives of this article
Objective: to selectively compile mysql-related functions based on configure parameters (if -- with-MySQL is available) during compilation.
Implementation: use the m4 script: ax_lib_mysql.m4.
2. how to use Autoconf to implement
Most of the things you think of have already been tried. This is no exception. There are many scripts and commands in Autoconf to help you do things. Here, we need to use ax_lib_mysql.m4 to help us. Put the file in the program/project directory, and add the following command in configure. ac to check the MySQL database files and versions:
The code is as follows:
M4_include (ax_lib_mysql.m4)
AX_LIB_MYSQL ()
AM_CONDITIONAL (BUILD_MYSQL_SUPPORT, test x $ MYSQL_VERSION! = X)
Note: AX_LIB_MYSQL () sets three variables, which can be found in Makefile. am is used directly, including MYSQL_CFLAGS, MYSQL_LDFLAGS, and MYSQL_VERSION. h predefines the macro HAVE_MYSQL; AM_CONDITIONAL (...) the variable BUILD_MYSQL_SUPPORT will be set based on whether MySQL support is enabled. this variable can be found in Makefile. am.
There are two ways to obtain the HAVE_MYSQL macro in the program source code: one is to directly include config. h; the other is to add-DHAVE_MYSQL in your program's CFLAGS. (Note: some variables can be used in Makefile. am, and others can be used in C source code)
Method 1: directly include config. h
The Autoconf tool stores all the predefined macros in config. h (default) and adds-DHAVE_CONFIG_H (via @ DEFS @) to the compiler options @). Because the ax_lib_mysql.m4 file contains the following code (if -- with-mysql is added and the corresponding mysql_config is found, the following code takes effect ):
The code is as follows:
AC_DEFINE ([HAVE_MYSQL], [1],
[Define to 1 if MySQL libraries are available])
Therefore, the macro definition in config. h is as follows:
The code is as follows:
/* Define to 1 if MySQL libraries are available */
# Define HAVE_MYSQL 1
In your source code (usually header files), add the following code:
The code is as follows:
# Ifdef HAVE_CONFIG_H
# Include> config. h <
# Endif
After that, you can use # ifdef HAVE_MYSQL... # endif in your source code.
Method 2: Add-DHAVE_MYSQL to the compiler option
Because the ax_lib_mysql.m4 file contains the variable definition MYSQL_CFLAGS/MYSQL_LDFLAGS/MYSQL_VERSION, you can simply add gcc compilation parameters based on these variables in Makefile. am. The statement is as follows:
The code is as follows:
If MYSQL_VERSION
XXX_CFLAGES =-DHAVE_MYSQL
Endif
After that, you can also use # ifdef HAVE_MYSQL... # endif in your source code.
Conclusion: one of the above two methods needs to modify Makefile. am and the other needs to modify the header file. you can decide what to do based on your preferences.
3.