Autoconf检查程序,如果没有找到则失败

问题描述 投票:15回答:6

我正在创建一个项目并使用GNU Autoconf工具进行配置和制作。我已经设置了所有的库检查和头文件检查,但似乎无法弄清楚如何检查系统上是否存在可执行文件,如果不存在则会失败。

我试过了:

AC_CHECK_PROG(TEST,testprogram,testprogram,AC_MSG_ERROR(Cannot find testprogram.))

当我configure它运行和输出:

Checking for testprogram... find: `testprogram. 15426 5 ': No such file or directory

但不会失败。

linux autoconf
6个回答
10
投票

尝试这是我刚从我的一个项目中取出的东西,它在路径中寻找一个叫做quantlib-config的东西:

# borrowed from a check for gnome in GNU gretl: def. a check for quantlib-config
AC_DEFUN(AC_PROG_QUANTLIB, [AC_CHECK_PROG(QUANTLIB,quantlib-config,yes)])
AC_PROG_QUANTLIB
if test x"${QUANTLIB}" == x"yes" ; then
    # use quantlib-config for QL settings
    [.... more stuff omitted here ...]
else
    AC_MSG_ERROR([Please install QuantLib before trying to build RQuantLib.])
fi

24
投票

我发现这是最短的方法。

AC_CHECK_PROG(FFMPEG_CHECK,ffmpeg,yes)
AS_IF([test x"$FFMPEG_CHECK" != x"yes"], [AC_MSG_ERROR([Please install ffmpeg before configuring.])])

4
投票

与上述类似,但具有通过导出条件变量也能够interact with automake的优点

AC_CHECK_PROG([ffmpeg],[ffmpeg],[yes],[no])
AM_CONDITIONAL([FOUND_FFMPEG], [test "x$ffmpeg" = xyes])
AM_COND_IF([FOUND_FFMPEG],,[AC_MSG_ERROR([required program 'ffmpeg' not found.])])

2
投票

使用AC_CHECK_PROG时,这是我遇到的最简洁的版本:

AC_CHECK_PROG(BOGUS,[bogus],[bogus],[no])
test "$BOGUS" == "no" && AC_MSG_ERROR([Required program 'bogus' not found.])

当程序丢失时,将生成此输出:

./configure
...cut...
checking for bogus... no
configure: error: Required program 'bogus' not found.

或者当与内置的autoconf程序检查结合使用时,请使用以下内容:

AC_PROG_YACC
AC_PROG_LEX

test "$YACC" == ":" && AC_MSG_ERROR([Required program 'bison' not found.])
test "$LEX" == ":" && AC_MSG_ERROR([Required program 'flex' not found.])

1
投票

在寻找这个问题的时候偶然发现,我应该注意,如果你想让你的程序只是在pathm中看一个运行时测试就足够了:

if ! which programname >/dev/null ; then
   AC_MSG_ERROR([Missing programname]
fi

1
投票

这不是一个简短的方法,它是一种普遍的purporse方法(虽然有几十个程序要检查它可能也是最短的方法)。它来自a project of mine(前缀NA_代表“Not Autotools”)。

通用宏

dnl  ***************************************************************************
dnl  NA_REQ_PROGS(prog1, [descr1][, prog2, [descr2][, etc., [...]]])
dnl
dnl  Checks whether one or more programs have been provided by the user or can
dnl  be retrieved automatically. For each program `progx` an uppercase variable
dnl  named `PROGX` containing the path where `progx` is located will be created.
dnl  If a program is not reachable and the user has not provided any path for it
dnl  an error will be generated. The program names given to this function will
dnl  be advertised among the `influential environment variables` visible when
dnl  launching `./configure --help`.
dnl  ***************************************************************************
AC_DEFUN([NA_REQ_PROGS], [
    m4_if([$#], [0], [], [
        AC_ARG_VAR(m4_translit([$1], [a-z], [A-Z]), [$2])
        AS_IF([test "x@S|@{]m4_translit([$1], [a-z], [A-Z])[}" = x], [
            AC_PATH_PROG(m4_translit([$1], [a-z], [A-Z]), [$1])
            AS_IF([test "x@S|@{]m4_translit([$1], [a-z], [A-Z])[}" = x], [
                AC_MSG_ERROR([$1 utility not found])
            ])
        ])
        m4_if(m4_eval([$# + 1 >> 1]), [1], [], [NA_REQ_PROGS(m4_shift2($*))])
    ])
])

样品用法

NA_REQ_PROGS(
    [find],             [Unix find utility],
    [xargs],            [Unix xargs utility],
    [customprogram],    [Some custom program],
    [etcetera],         [Et cetera]
)

所以在Makefile.am你可以做到

$(XARGS)

要么

$(CUSTOMPROGRAM)

等等。

特征

  • 它在最终用户启动./configure --help时可见的“有影响的环境变量”中宣传程序,以便可以提供程序的替代路径
  • 创建一个名称与程序名称相同的bash变量,但是大写,包含程序所在的路径
  • 如果找不到任何给定的程序并且用户没有为它们提供任何替代路径,则抛出En错误
  • 宏可以采用无限(一对)参数

什么时候应该使用它

  1. 当要测试的程序对于编译项目至关重要时,用户必须能够为它们提供替代路径,并且如果至少有一个程序根本不可用,则必须抛出错误
  2. 当条件#1适用于多个单个程序时,在这种情况下,不需要编写通用宏,您只需使用自己的自定义代码
© www.soinside.com 2019 - 2024. All rights reserved.