首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译lex生成的文件时errno.h (及更多)中的错误

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

Stack Overflow用户
提问于 2018-08-25 18:39:05
回答 1查看 464关注 0票数 0

我目前正在收到错误消息:

代码语言:javascript
复制
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使用命令生成的文件时:

代码语言:javascript
复制
gcc lex.yy.c

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

代码语言:javascript
复制
%%
"hello world"   printf("goodbye");
.               ;
%%

当我试图编译由lex或flex生成的任何文件时,都会出现这个问题(我尝试了这两种方法)

正如我所提到的,还有更多的错误,但是也许修复这个错误会解决其他的一些错误。在查找了errno.h中的一些常见错误并且找不到任何有用的错误之后,我在这里问。

我正在使用Ubuntu16.04LTS。如果你想要/需要更多关于这个问题的信息,请告诉我,我会尽力帮助你的。

(谢谢你的建议:)

编辑“rici”:

文件的前21行如下:

代码语言:javascript
复制
    #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来比较这两个文件。

那些在我档案里但不在你档案里的东西:

代码语言:javascript
复制
#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 world包含了以下错误:

代码语言:javascript
复制
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词条.yy.c“时抛出的错误的全部转储:https://gist.github.com/raygarner/0601e57f5be21e16e0ae4ee34643b121

编辑@sepp2k:

早些时候,我在VM中debian 9的新安装上测试了这个完全相同的编译过程,并且在修复errno.h之后,在Ubuntu上得到了完全相同的错误。

如下所示:

代码语言:javascript
复制
/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-25 21:29:17

正如我们在注释中所发现的,最初的错误是由于您的系统上的errno.h出错,您通过重新安装文件修复了这个错误。

解决了这个问题后,您的代码将编译得很好,但不会因为缺少yywrapmain函数而链接为一个独立的应用程序。您可以通过定义yywrap或使用%option noyywrap来修复前者。后者可以通过定义主函数或针对定义main的对象文件(如果lexer是已经定义了自己的主函数的较大项目的一部分)进行链接来修复。

还可以通过链接-lfl来解决这两个问题,后者定义了yywrapmain函数,以便从stdin或argv中读取文件名,然后将结果标记打印到stdout。当然,这只对测试目的有用,因为在实际项目中,您希望做的比在主项目中做的更多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52020213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档