Table of Contents
1. 本文的目标
2. 如何利用Autoconf实现
方法一:直接include config.h
方法二:编译器选项新增-DHAVE_MYSQL
3. 更多关于ax_lib_mysql.m4的使用
常见的configure写法
如果对最低版本有要求
修改--with-mysql的默认行为
4. 更一般的DEBUG选项
参考链接
Home Database Mysql Tutorial 使用Autoconf检测MySQL相关的软件包

使用Autoconf检测MySQL相关的软件包

Jun 07, 2016 pm 04:39 PM
mysql use Detection Related software package

在你的程序(或者工程)中,如果编译阶段需要检测当前环境中是否存在MySQL客户端相关的库文件时,你可以使用Autoconf来帮你完成这个工作,轻盈、优雅、无痛。阅读本文需要了解简单GNU Autoconf使用。 1. 本文的目标 目的:编译时,根据configure参数(如果有--w

在你的程序(或者工程)中,如果编译阶段需要检测当前环境中是否存在MySQL客户端相关的库文件时,你可以使用Autoconf来帮你完成这个工作,轻盈、优雅、无痛。阅读本文需要了解简单GNU Autoconf使用。

1. 本文的目标

目的:编译时,根据configure参数(如果有--with-mysql),选择性编译对应的MySQL相关的功能。

实现:使用已经写好的m4脚本:ax_lib_mysql.m4

2. 如何利用Autoconf实现

大部分你想到的事情都已经有人做过尝试了。这件事情也不例外,Autoconf中有很多脚本和指令帮你做事情。这里,需要使用ax_lib_mysql.m4来帮助我们。先把该文件放到程序/工程目录中,并在configure.ac中新增如下指令来检测MySQL库文件和版本:

m4_include(ax_lib_mysql.m4)
AX_LIB_MYSQL()
AM_CONDITIONAL(BUILD_MYSQL_SUPPORT, test x$MYSQL_VERSION != x)

说明:AX_LIB_MYSQL()设置了三个变量,可以在Makefile.am中直接使用,分别是MYSQL_CFLAGS、MYSQL_LDFLAGS、MYSQL_VERSION,另外还会在config.h中预定义宏HAVE_MYSQL;AM_CONDITIONAL(...)则会根据是否需要开启MySQL支持,来设置变量BUILD_MYSQL_SUPPORT,这个变量可以在Makefile.am中使用。

在程序源代码中一般有两种方式可以获取HAVE_MYSQL宏的方式:一个是直接包含config.h;另一个是在你程序的CFLAGS中新增-DHAVE_MYSQL。(注意:有的变量是可以在Makefile.am中使用,有的则是可以在C源代码中使用)

方法一:直接include config.h

Autoconf工具会将所有的预定义宏存放在config.h(默认情况)中,并在编译器选项中新增-DHAVE_CONFIG_H(通过@DEFS@)。因为文件ax_lib_mysql.m4中,包含了如下代码(如果加上--with-mysql并且找到了对应mysql_config,那么如下代码生效):

AC_DEFINE([HAVE_MYSQL], [1],
[Define to 1 if MySQL libraries are available])

所以,config.h中会有对应的宏定义:

/* Define to 1 if MySQL libraries are available */
#define HAVE_MYSQL 1

在你的源代码中(一般是头文件),新增如下代码:

#ifdef HAVE_CONFIG_H
#include >config.h #endif

这之后,就可以在你的源代码中,使用#ifdef HAVE_MYSQL ... #endif这样的写法了

方法二:编译器选项新增-DHAVE_MYSQL

因为文件ax_lib_mysql.m4包含了变量定义MYSQL_CFLAGS/MYSQL_LDFLAGS/MYSQL_VERSION,所以,简单的可以在Makefile.am中,直接根据这些变量来新增gcc编译参数。类似如下写法:

if MYSQL_VERSION
XXX_CFLAGES= -DHAVE_MYSQL
endif

这之后,也可以在你的源代码中,使用#ifdef HAVE_MYSQL ... #endif这样的写法了

小结:上面两种方法一个需要修改Makefile.am、一个需要修改头文件,可以根据个人喜好来决定怎么做。

3. 更多关于ax_lib_mysql.m4的使用

常见的configure写法

有了上面的设置,程序就可以通过如下的方式来确定是否将MySQL客户端的支持编译到源代码中:

./configure --with-mysql
...
./configure --with-mysql[=no|yes]
...
./configure --with-mysql[=/YOUR_ENV_PATH/mysql_config] #如果mysql_config不在当前的$PATH中,则需要显示指定。
...

如果对最低版本有要求

另外,如果你对MySQL版本有要求,例如,你希望只有检测到5.5以上的MySQL客户端,才编译对MySQL的支持,则可以在configure.ac中这样使用AX_LIB_MYSQL:

AX_LIB_MYSQL(5.5.18)

修改--with-mysql的默认行为

这里意思是说,如果在configure中没有--with-mysql选项时,则编译时不加上对MySQL的支持(如果写了),也就是说如下两种写法意思相同:

./configure --with-mysql=no
./configure

ax_lib_mysql.m4的默认行为并非如此,需要对其代码做小小的修改:

1

2

3

4

5

6

7

8

@@ -61,7 +61,7 @@

             MYSQL_CONFIG="$withval"

         fi

         ],

-        [want_mysql="yes"]

+        [want_mysql="no"]

     )

     AC_ARG_VAR([MYSQL_CONFIG], [Full path to mysql_config program])

Copy after login

这样就如愿了。

4. 更一般的DEBUG选项

其实使用Autoconf这种用法更一般的是开启或者关闭DEBUG选项。这个实现会比上面简单很多。

目标:编译时,根据configure参数(如果有--enable-debug),则执行程序中#ifdef DEBUG ... #endif。(经常看到这样的写法吧)

相比上面的--with-mysql这个就简单多了(没有版本信息、不需要找mysql_config等),所以实现也简单多了,只需在你的configure.ac中新增如下代码:

1

2

3

4

5

6

7

8

9

10

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")

Copy after login

如果configure时,带有参数--enable-debug,则设置调用AM_CONDITIONAL设置遍历DEBUG。这样就可以在Makefile中根据遍历DEBUG,来选择性的新增编译参数-DDEBUG,所以配套的还需要再Makefile.am中新增:

if DEBUG
XXX_CFALGS=... -DDEBUG
else
XXX_CFALGS=...
fi

这时,你的代码中就可以写#ifdef DEBUG ... #endif了。

另一种包含config.h的方法跟前面类似,只不过需要将AM_CONDITIONAL那里换成:

if test x"$debug" = x"true"
AC_DEFINE([HAVE_MYSQL], [1],
[Define to 1 if MySQL libraries are available])
fi

那么程序代码中include >config.h

参考链接

Creating Your Own Configuration

Autotools 实例分析

Usage of AM_CONDITIONAL

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

How to share the same page on the PC and mobile side and handle cache issues? How to share the same page on the PC and mobile side and handle cache issues? Apr 01, 2025 pm 01:57 PM

How to share the same page on the PC and mobile side and handle cache issues? In the nginx php mysql environment built using the Baota background, how to make the PC side and...

PHP optimistic locking combined with transaction deduction balance failed: How to ensure that the balance is correctly deducted in concurrency situations? PHP optimistic locking combined with transaction deduction balance failed: How to ensure that the balance is correctly deducted in concurrency situations? Mar 31, 2025 pm 11:42 PM

Detailed explanation of the problem of deducting balances in combination with PHP optimistic locks and transactions in this article will analyze in detail a balance deduction using PHP, optimistic locks and database transactions, only...

Monitoring Redis Droplets Using Redis Exporter Service Monitoring Redis Droplets Using Redis Exporter Service Jan 06, 2025 am 10:19 AM

Effective monitoring of Redis databases is essential for maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a robust utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you establish a monitoring solution seamlessly. By following this tutorial, you’ll achieve a fully operational monitoring setup to effectively monitor the performance metrics of your Redis database.

Is Debian Strings compatible with multiple browsers Is Debian Strings compatible with multiple browsers Apr 02, 2025 am 08:30 AM

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness

See all articles