首页 > 解决方案 > autoconf中单方括号和双方括号的区别

问题描述

那么autoconf中的单方括号和双方括号有什么区别呢?

Autoconf 文档显示以下示例:

AC_CHECK_TYPES([float_t], [], [], [[#include <math.h>]])

恕我直言,它也适用于单括号:

AC_CHECK_TYPES([float_t], [], [], [#include <math.h>])

标签: autoconf

解决方案


如果你使用[[ ]]你的宏参数,你可以在宏参数中随意使用[]。另一方面,如果您使用[]宏参数,[并在宏参数中]保留它们的特殊 autoconf/m4 含义。

如果宏参数中的实际文本包含括号,则差异变得明显,例如

dnl Minimum working example configure.ac. To run:
dnl   touch Makefile.am && autoreconf -vis . && ./configure

AC_PREREQ([2.69])
AC_INIT([stackoverflow53609622], [0.0.1], [bugs@example.com])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])

AC_PROG_CC

AC_MSG_CHECKING([compile example 1])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
const char hw[] = "Hello, World\n";
const char hs[] = "Hello, Stackoverflow\n";
]])], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])])

AC_MSG_CHECKING([compile example 2])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
const char hw[[]] = "Hello, World\n";
const char hs[[]] = "Hello, Stackoverflow\n";
])], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])])

AC_MSG_CHECKING([compile example 3])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
const char hw[] = "Hello, World\n";
const char hs[] = "Hello, Stackoverflow\n";
])], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

示例 1 和 2 都将测试编译 C 程序

const char hw[] = "Hello, World\n";
const char hs[] = "Hello, Stackoverflow\n";

但是示例 3 将测试编译损坏的 C 程序

const char hw = "Hello, World\n";
const char hs = "Hello, Stackoverflow\n";

这将无法编译(请参阅config.log编译器错误消息)。

但是,如果您将不平衡的括号放入宏参数文本中,m4仍然会对宏参数的开始或结束位置感到非常困惑。AFAIK,解决这个问题的唯一方法是使用四边形(@<:@for[@:>@for ]):

AC_MSG_CHECKING([compile example 4])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
const char hw[] = "Hello, World @<:@-:\n";
const char hs[] = "Hello, Stackoverflow\n";
]])], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])])

AC_MSG_CHECKING([compile example 5])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
const char hw[] = "Hello, World :-@:>@\n";
const char hs[] = "Hello, Stackoverflow\n";
]])], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])])

以下操作将在某个时间autoreconfconfigure某个时间失败,这表明出现了非常错误的情况,您应该非常清楚地避免这种情况。

AC_MSG_CHECKING([compile example 6])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
const char hw[] = "Hello, World :-[\n";
const char hs[] = "Hello, Stackoverflow\n";
]])], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])])

AC_MSG_CHECKING([compile example 7])
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
const char hw[] = "Hello, World :-]\n";
const char hs[] = "Hello, Stackoverflow\n";
]])], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])])

如需进一步阅读,您可以从https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/M4-Quotation.html开始并从那里深入挖掘。


推荐阅读