AM_PROG_LEX 和未定义的 yywrap

AM_PROG_LEX and undefined yywrap

我的程序使用记录的 autoconf 宏 AM_PROG_LEX。它在 RHEL 6.5 和其他发行版上构建良好,但在 RHEL 6.6 及更高版本上失败。

配置脚本无法编译其测试。当它使用 -ll-lfl 尝试 gcc 时,链接失败:

/usr/bin/ld: cannot find -lfl

当它在没有额外库的情况下尝试 gcc 时,链接失败并显示:

undefined reference to `yywrap'

libfl.alibfl.so 在这些系统的官方回购中缺失。在 RHEL 6.5 上它是 flex 包的一部分。

RHEL 6.5
configure:5334: checking whether yytext is a pointer
configure:5351: gcc -o conftest -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -O0   conftest.c -lfl  >&5
configure:5351: $? = 0
configure:5359: result: yes


RHEL 6.8
configure:5196: checking whether yytext is a pointer
configure:5217: gcc -o conftest -g -O2   conftest.c   >&5
/tmp/ccNJtVgv.o: In function `input':
/home/git/rpmbuild/BUILD/snacc-1.3.1_16_g23ba7a6/lex.yy.c:1168: undefined reference to `yywrap'
/tmp/ccNJtVgv.o: In function `yylex':
/home/git/rpmbuild/BUILD/snacc-1.3.1_16_g23ba7a6/lex.yy.c:867: undefined reference to `yywrap'
/tmp/ccNJtVgv.o: In function `main':
/home/git/rpmbuild/BUILD/snacc-1.3.1_16_g23ba7a6/conftest.l:17: undefined reference to `yywrap'
collect2: ld returned 1 exit status
configure:5224: $? = 1
configure: failed program was:
...
configure:5246: result: no

libfl 包含两个且只有两个函数,这两个函数在 flex 的生产使用中通常都是不必要的:

int main() { extern int yylex(void); while (yylex()) ; return 0; }
int yywrap(void) { return 1; }

如果您使用

选项,则不需要 yywrap 实现(实质上禁用 yywrap 功能)
%option noyywrap

在您的 flex 定义中,或者如果您将命令行选项 --noyywrap 传递给 flex。

对于 quick-and-dirty flex scanners,或者为了调试,有时可以使用 libfl 来填写上面的函数会很方便。但它也会在提供 32 位和 64 位环境的系统上产生问题。出于这个原因,libfl 已于 2014 年从 RHEL flex rpm 中删除。有关详细信息,请参阅此 RedHat bug fix advisory

因此您可以安装适当的 flex-devel rpm 以便 libfl 可用。或者您可以使用上面的代码自行编译它(准确地 不是您在 flex 源包中找到的源代码,但应该生成完全相同的库)。

或者您可以尝试修复 autoconf,使其不依赖于 libfl。它过去没有任何这种依赖性;如果找不到 libfl,它会假设它不是正在编译的程序所必需的。

解决方法是安装包含 libfl.aflex-devel 包。 RHEL 版本仅供订阅者使用。备选方案是 CentOS 软件包或从源代码重新编译。