编译lex生成的文件时errno.h(及更多)中的错误

时间:2018-08-25 18:39:05

标签: c flex-lexer lex

我当前收到错误消息:

In file included from /usr/include/errno.h:35:0,
                 from lex.yy.c:21:
/usr/include/x86_64-linux-gnu/bits/errno.h:50:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
 extern int *__errno_location (void) __THROW __attribute__ ((__const__));
 ^
尝试使用以下命令编译lex生成的文件时,

(以及许多其他功能):

gcc lex.yy.c

lex.l(传递给lex以生成lex.yy.c的文件)源:

%%
"hello world"   printf("goodbye");
.               ;
%%

当我尝试编译lex或flex生成的任何文件(我都尝试过)时,会出现此问题

正如我提到的那样,还有很多其他错误,但也许修复此错误将解决其他一些错误。在查找errno.h的一些常见错误并没有任何用处之后,我在这里询问。

我正在使用Ubuntu 16.04 LTS。让我知道您是否需要/需要有关该问题的更多信息,我们将尽力为您提供帮助。

感谢您的任何建议:)

编辑“ rici”:

我的lex.yy.c文件的前21行如下:

    #line 3 "lex.yy.c"

    #define  YY_INT_ALIGNED short int

    /* A lexical scanner generated by flex */

    #define FLEX_SCANNER
    #define YY_FLEX_MAJOR_VERSION 2
    #define YY_FLEX_MINOR_VERSION 6
    #define YY_FLEX_SUBMINOR_VERSION 0
    #if YY_FLEX_SUBMINOR_VERSION > 0
    #define FLEX_BETA
    #endif

    /* First, we deal with  platform-specific or compiler-specific issues. */

    /* begin standard C headers. */
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    /* end standard C headers. */

编辑@ sepp2k:

我用vimdiff比较了两个文件。

我的文件中没有的东西:

#ifdef __cplusplus

/* The "const" storage-class-modifier is valid. */
#define YY_USE_CONST

#else   /* ! __cplusplus */

/* C99 requires __STDC__ to be defined as 1. */
#if defined (__STDC__)

#define YY_USE_CONST

#endif  /* defined (__STDC__) */
#endif  /* ! __cplusplus */

#ifdef YY_USE_CONST

===============================================================

#include <string.h>
#include <errno.h>
#include <stdlib.h>
#line 476 "lex.yy.c"

您的文件中确实没有属于我的文件

其他任何差异似乎只是格式上的差异。

我还在标准C程序中测试了四个头文件(请参阅现在的意思),并且可以确认它是导致错误的errno.h。

包含errno.h的C语言的hello世界引发了以下错误:

In file included from /usr/include/errno.h:35:0,
                 from test.c:2:
/usr/include/x86_64-linux-gnu/bits/errno.h:50:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘extern’
 extern int *__errno_location (void) __THROW __attribute__ ((__const__));
 ^
In file included from test.c:4:0:
/usr/include/stdio.h:13:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
 int printf(const char* __restrict, ...);
 ^

编辑@rici:

这是我运行“ gcc lex.yy.c”时抛出的错误的完整转储: https://gist.github.com/raygarner/0601e57f5be21e16e0ae4ee34643b121

编辑@ sepp2k:

之前,我在VM上全新安装的debian 9上测试了完全相同的编译过程,并且在修复errno.h之后,在Ubuntu上遇到了与此处完全相同的错误

这是它的样子:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:0)

正如我们在评论中发现的那样,原始错误是由于系统上的errno.h混乱所致,您可以通过重新安装文件来解决此问题。

解决了该问题后,您的代码将可以正常编译,但由于缺少yywrapmain函数,因此无法作为独立应用程序链接。您可以通过定义yywrap或使用%option noyywrap来修复前者。可以通过定义主函数或链接到定义main的目标文件来修复后者(如果词法分析器是已经定义了其主函数的较大项目的一部分)。

您还可以通过链接-lfl来解决这两个问题,该链接定义了yywrapmain函数以从stdin读取或从argv读取文件名,然后打印结果令牌到标准输出。当然,这仅对测试有用,因为您要在实际项目中做的比主项目还要多。