Autoconf 是一个用于生成自动配置脚本的工具,它可以帮助你的软件适应不同的 Unix-like 系统。以下是关于如何定制 Autoconf 检查的详细说明:
Autoconf 提供了许多预定义的检查宏,例如:
AC_CHECK_HEADERS([stdio.h stdlib.h])
AC_CHECK_FUNCS([strdup malloc])
AC_CHECK_LIB([m], [sqrt])
你可以创建自己的检查:
AC_MSG_CHECKING([for custom feature])
# 执行检查代码
if test "x$have_custom_feature" = xyes; then
AC_MSG_RESULT([yes])
AC_DEFINE([HAVE_CUSTOM_FEATURE], [1], [Define if custom feature is available])
else
AC_MSG_RESULT([no])
fi
在 configure.ac
中定义自己的宏:
AC_DEFUN([MY_CUSTOM_CHECK],
[
AC_MSG_CHECKING([for my custom check])
# 检查逻辑
if some_condition; then
AC_MSG_RESULT([yes])
$1
else
AC_MSG_RESULT([no])
$2
fi
])
然后在 configure.ac
中调用:
MY_CUSTOM_CHECK(
[AC_DEFINE([HAVE_MY_FEATURE], [1], [Define if feature is available])],
[AC_MSG_WARN([Feature not available])]
)
AC_MSG_CHECKING([if program works correctly])
# 尝试运行程序并检查输出
if ./some_program --test | grep -q "expected output"; then
AC_MSG_RESULT([yes])
have_program=yes
else
AC_MSG_RESULT([no])
have_program=no
fi
使用 AC_CACHE_CHECK
可以缓存检查结果,避免重复检查:
AC_CACHE_CHECK([for custom feature], [ac_cv_custom_feature],
[
# 检查代码
if test_condition; then
ac_cv_custom_feature=yes
else
ac_cv_custom_feature=no
fi
])
if test "x$ac_cv_custom_feature" = xyes; then
AC_DEFINE([HAVE_CUSTOM_FEATURE], [1], [Define if custom feature is available])
fi
AC_MSG_CHECKING([for compiler support of C11])
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM(
[[#include <stdalign.h>]],
[[alignas(16) int x;]]
)],
[AC_MSG_RESULT([yes])
AC_DEFINE([HAVE_C11], [1], [Define if compiler supports C11])],
[AC_MSG_RESULT([no])]
)
AC_MSG_CHECKING([for endianness])
AC_RUN_IFELSE(
[AC_LANG_PROGRAM(
[[#include <stdint.h>]],
[[
volatile uint32_t i=0x01234567;
return (*((uint8_t*)(&i))) == 0x67 ? 0 : 1;
]]
)],
[AC_MSG_RESULT([little endian])
AC_DEFINE([WORDS_LITTLEENDIAN], [1], [Define if system is little endian])],
[AC_MSG_RESULT([big endian])]
)
对于复杂的检查,可以创建测试程序:
AC_MSG_CHECKING([for working pthreads])
AC_LINK_IFELSE(
[AC_LANG_PROGRAM(
[[#include <pthread.h>]],
[[
pthread_t th;
pthread_create(&th, 0, 0, 0);
pthread_join(th, 0);
]]
)],
[AC_MSG_RESULT([yes])
LIBS="$LIBS -lpthread"],
[AC_MSG_RESULT([no])
AC_MSG_ERROR([pthreads not working])]
)
根据检查结果设置变量:
AC_CHECK_HEADER([some_header.h],
[have_some_header=yes],
[have_some_header=no])
AM_CONDITIONAL([HAVE_SOME_HEADER], [test "x$have_some_header" = xyes])
AC_MSG_CHECKING
和 AC_MSG_RESULT
提供清晰的输出AC_CACHE_CHECK
AC_MSG_ERROR
或 AC_MSG_WARN
处理关键缺失通过以上方法,你可以灵活地定制 Autoconf 的检查过程,确保你的软件能够在各种系统上正确配置和编译。