如果要给configure生成像--enable-XXXX,--disable-XXXX,--with-XXXX,--without-XXXX这样的参数,就需在configure.ac里用到AC_ARG_ENABLE和AC_ARG_WITH这两个宏,AC_ARG_ENABLE和AC_ARG_WITH语法如下:
AC_ARG_ENABLE(option-name, help-string, action-if-present, action-if-not-present)
AC_ARG_WITH (package, help-string, [action-if-given], [action-if-not-given])
比如下面这个例子,./configure 时如果加了 --enable-debug,就会执行enable_debug="yes",否则执行enable_debug="no":
AC_ARG_ENABLE(
[debug],
[AS_HELP_STRING([--enable-debug],[enable debugging code and output])],
[enable_debug="yes"],
[enable_debug="no"]
)
后面再根据enable_debug这个变量做处理:定义makefile.am宏、修改(或定义)makefile.am变量、代码宏。
if test "${enable_debug}" = "yes"; then
#修改makefile CFLAGS变量(在Makefile.am 里使用)
CFLAGS="${CFLAGS} -ggdb3 -DDEBUG"
#定义DEBUG_CFLAGS变量(在Makefile.am 里使用)
DEBUG_CFLAGS="-ggdb3 -DDEBUG"
AC_SUBST([DEBUG_CFLAGS])
#在config.h中定义ENABLE_DEBUG宏(在代码里里使用)
AC_DEFINE(
[ENABLE_DEBUG],
[1],
[Define to 1 if debug should be enabled]
)
fi
#根据条件定义makefile宏(在Makefile.am 里使用)
AM_CONDITIONAL([DEBUG], [test "${enable_debug}" = "yes"])
Makefile.am使用宏:
if DEBUG
bin_PROGRAMS = helloworld debug
debug_SOURCES=src/debug.c
else
bin_PROGRAMS = helloworld
endif
if DEBUG
helloworld_SOURCES = src/helloworld.c \
src/hello-debug.c
else
helloworld_SOURCES = src/helloworld.c
endif
带参数的用法如下:
如果configure 没有加--disable-plugins,执行 disable_plugins="no",后面的编译宏和代码宏都不会定义。
如果configure 加了--disable-plugins 或 --disable-plugins=xxx,只要参数不是"yes", disable_plugins 就 !="no",在Makefile.am中就会定义DISABLE_PLUGINS宏,在 config.h中就会定义 DISABLE_PLUGINS 为 1。
AC_ARG_ENABLE(
[plugins],
[AS_HELP_STRING([--disable-plugins], [Disable external reparse point
plugins for the ntfs-3g FUSE driver])],
[if test x${enableval} = "xyes"; then disable_plugins="no"; fi],
[disable_plugins="no"]
)
test "${disable_plugins}" != "no" && AC_DEFINE([DISABLE_PLUGINS], [1], [Define to 1 for disabling reparse plugins])
AM_CONDITIONAL([DISABLE_PLUGINS], [test "${disable_plugins}" != "no"])
AC_ARG_WITH的用法:
AC_ARG_WITH(pkgconfigdir,
AS_HELP_STRING([[[--with-pkgconfigdir]]],
[Use the specified pkgconfig dir (default is libdir/pkgconfig)]),
[pkgconfigdir=${withval}],
[pkgconfigdir='${libdir}/pkgconfig'])
AC_SUBST([pkgconfigdir])
AC_MSG_NOTICE([[pkgconfig directory is ${pkgconfigdir}]])
参考:
大型项目使用Automake/Autoconf完成编译配置:
https://blog.youkuaiyun.com/fd315063004/article/details/7785504
使用autotools生成Makefile学习笔记:
https://geesun.github.io/posts/2015/02/autotool.html