A tutorial on using autoconf to detect Mysql software packages _mysql

Source: Internet
Author: User
Tags mysql client mysql version

In your program (or engineering), if the compile phase needs to detect the existence of MySQL client-related library files in the current environment, you can use autoconf to help you do this work, lightweight, elegant, painless. Read this article to learn about the simple GNU autoconf use.
1. The objective of this article

Objective: Compile-time, according to the Configure parameters (if there is--with-mysql), selectively compile the corresponding mysql-related functions.

Implementation: Using a M4 script that has already been written: AX_LIB_MYSQL.M4
2. How to use autoconf to realize

Most of the things you think about are already being tried. This is no exception, and there are a lot of scripts and instructions in autoconf to help you do things. Here, you need to use AX_LIB_MYSQL.M4 to help us. First put the file into the program/engineering directory, and add the following instructions in CONFIGURE.AC to detect MySQL library files and versions:

Copy Code code as follows:
M4_include (AX_LIB_MYSQL.M4)
Ax_lib_mysql ()
Am_conditional (build_mysql_support, test x$mysql_version!= x)

Description: Ax_lib_mysql () set three variables, can be used directly in makefile.am, respectively, Mysql_cflags, Mysql_ldflags, Mysql_version, In addition, a predefined macro have_mysql;am_conditional (...) is also predefined in config.h. You will set the variable Build_mysql_support, depending on whether you need to turn on MYSQL support, which can be used in makefile.am.

There are generally two ways to get Have_mysql macros in the program source code: One is to include config.h directly, and the other is to add-dhave_mysql to the cflags of your program. (Note: Some variables can be used in makefile.am, others can be used in C source code)
method One: direct include config.h

The Autoconf tool stores all predefined macros in Config.h (by default) and adds-dhave_config_h (through @defs@) to compiler options. Because the file AX_LIB_MYSQL.M4 contains the following code (if you add--with-mysql and find the corresponding mysql_config, the following code takes effect):

Copy Code code as follows:
Ac_define ([Have_mysql], [1],
[Define to 1 if MySQL libraries are available])

Therefore, the config.h will have the corresponding macro definition:

Copy Code code as follows:
/* Define to 1 if MySQL libraries are available * *
#define HAVE_MYSQL 1

In your source code (typically a header file), add the following code:

Copy Code code as follows:
#ifdef HAVE_CONFIG_H
#include >config.h<
#endif

After that, you can use #ifdef Have_mysql in your source code ... #endif这样的写法了
method Two: compiler option new-dhave_mysql

Because file AX_LIB_MYSQL.M4 contains variable definition mysql_cflags/mysql_ldflags/mysql_version, it is easy to add GCC compilation parameters directly in makefile.am based on these variables. Similar to the following wording:

Copy Code code as follows:
If mysql_version
xxx_cflages=-dhave_mysql
endif

After that, you can also use #ifdef have_mysql in your source code ... #endif这样的写法了

Summary: The above two methods need to modify makefile.am, a need to modify the header file, you can decide according to personal preferences how to do.
3. More about the use of AX_LIB_MYSQL.M4
The Common Configure writing

With the above settings, the program can determine whether to compile the MySQL client support into the source code in the following ways:

Copy Code code as follows:
./configure--with-mysql
...
./configure--with-mysql[=no|yes]
...
./configure--with-mysql[=/your_env_path/mysql_config] #如果mysql_config不在当前的 $PATH, you need to display the specified.
...

If the minimum version is required

In addition, if you have requirements for MySQL version, for example, you want only to detect more than 5.5 of the MySQL client to compile the support for MySQL, you can use the Ax_lib_mysql in Configure.ac:
Ax_lib_mysql (5.5.18)
Modifying the default behavior of--with-mysql

This means that if there is no--with-mysql option in the Configure, then compile without support for MySQL (if it is written), which means the following two ways:

Copy Code code as follows:
./configure--with-mysql=no
./configure

The default behavior of AX_LIB_MYSQL.M4 is not the case, and you need to make a small change to its code:

Copy Code code as follows:
@@ -61,7 +61,7 @@
Mysql_config= "$withval"
Fi
],
-[want_mysql= "yes"]
+ [want_mysql= "no"]
)
Ac_arg_var ([Mysql_config], [Full path to Mysql_config])

That's the way it is.
4. More general Debug Options

In fact, using autoconf is more commonly used to turn on or off the debug option. This implementation will be much simpler than the above.

Target: At compile time, according to the Configure parameter (if there is--enable-debug), #ifdef Debug in the execution program ... #endif. (often see this way of writing it)

Compared to the above--with-mysql this is much simpler (no version information, no need to find mysql_config, etc.), so the implementation is much simpler, just add the following code to your CONFIGURE.AC:

Copy Code code as follows:
Ac_arg_enable (Debug,
As_help_string ([--enable-debug],
[Enable debugging, Default:no]),
[Case ' ${enableval} ' in
YES) debug=true;;
NO) Debug=false;
*) Ac_msg_error ([Bad Value ${enableval} for--enable-debug]);;
ESAC],
[Debug=false])
Am_conditional (DEBUG, Test x "$debug" = X "true")

If configure with a parameter--enable-debug, the setting calls the Am_conditional setting to traverse Debug. This can be in makefile according to traverse Debug, to select the new compilation parameters-ddebug, so the matching needs to be added to the makefile.am:

Copy Code code as follows:
If DEBUG
Xxx_cfalgs= ...-ddebug
Else
Xxx_cfalgs= ...
Fi

At this point, you can write #ifdef DEBUG in your code ... #endif了.

Another method that contains config.h is similar to the previous one, except that the am_conditional is replaced with the following:

Copy Code code as follows:
if test X "$debug" = X "true"
Ac_define ([Have_mysql], [1],
[Define to 1 if MySQL libraries are available])
Fi

Then the include >config.h< in the program code is OK.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.