首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

Autoconf 檢測 MySQL 客戶端庫完整教程:ax_lib_mysql.m4 實戰

     編輯:MySQL 資料庫相關知識

目標

在建構系統中依 --with-mysql 參數,選擇性啟用 MySQL 客戶端相關功能編譯。

核心實作步驟

1. 引入現成 M4 巨集

下載 ax_lib_mysql.m4 放入專案目錄,在 configure.ac 中:

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

2. AX_LIB_MYSQL 提供的變數

原始碼中使用 HAVE_MYSQL 的兩種方式

方法一:包含 config.h(推薦)

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_MYSQL
// MySQL 相關程式碼
#endif

方法二:透過編譯器旗標傳遞

if MYSQL_VERSION
MY_CFLAGS = -DHAVE_MYSQL
endif

進階 configure 參數用法

./configure --with-mysql              # 自動尋找 mysql_config
./configure --with-mysql=yes          # 明確啟用
./configure --with-mysql=no           # 明確停用
./configure --with-mysql=/path/to/mysql_config  # 指定路徑
./configure --with-mysql=5.5.18       # 要求最低版本

修改預設行為:無參數時停用 MySQL

編輯 ax_lib_mysql.m4,將預設 want_mysql="yes" 改為 "no":

- [want_mysql="yes"]
+ [want_mysql="no"]

通用 DEBUG 選項實作(更簡單)

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

配合 Makefile.am:

if DEBUG
AM_CFLAGS = -DDEBUG
endif
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
變數用途
MYSQL_CFLAGS編譯器包含路徑
MYSQL_LDFLAGS連結器庫路徑與庫名
MYSQL_VERSION偵測到的版本號
HAVE_MYSQLconfig.h 預定義巨集