如何删除以下"隐含的函数声明"警告?

ide*_*ikz 12 c lex

如何使用gcc编译lex文件而不接收以下警告?

lex.yy.c: In function `yy_init_buffer':
lex.yy.c:1688: warning: implicit declaration of function `fileno'
lex.l: In function `storeLexeme':
lex.l:134: warning: implicit declaration of function `strdup'
Run Code Online (Sandbox Code Playgroud)

这些是我包含的库.

%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
%}
Run Code Online (Sandbox Code Playgroud)

函数yy_init_buffer不在文件中.以下是函数storeLexeme.

 int storeLexeme() {
for (int i = 0; i < count; i++) {
    char *curr = *(symbolTable + i); 
    if (strcmp(curr, yytext) == 0) {
        return i;
    }
}
char *lexeme = (char *)malloc(sizeof(char *));
lexeme = (char *)strdup(yytext);
symbolTable[count] = lexeme;
count++;
return (count - 1);
 }
Run Code Online (Sandbox Code Playgroud)

如何删除警告?

pax*_*blo 11

既不是ISO C strdup也不fileno是ISO C函数,它们都是POSIX的一部分.

现在,您的平台上是否可以使用它们取决于您的平台.


如果您使用的是Microsoft工具,则可能需要考虑_fileno后者(在VC2005 fileno中已弃用).这里strdup可以找到相当优秀的版本.

虽然,用我的代码吹了我自己的号角,你也可以使用,_strdup因为它取代了也弃用了strdup :-)

这些应该有希望的工作没关系,-是因为他们是在stdio.hstring.h两个你已经在使用包含文件.


如果您使用的是UNIX衍生产品,那么这些功能应该在stdio.h(for fileno)和string.h(for strdup)中可用.鉴于您看起来已经包含了这些文件,问题可能在其他地方.

一种可能性是,如果你正在编译一个严格的模式,如__STRICT_ANSI__在gcc),其中既没有定义.

你应该看看你所产生的顶部lex.yy.clex.l文件,以确认头文件被列入,并检查你传递到编译器的命令行参数.


小智 9

我建议这个选项(告诉编译器你正在使用POSIX):

#define _POSIX_C_SOURCE 1
Run Code Online (Sandbox Code Playgroud)

人们似乎近年来已经收紧了功能控制,并且希望当一致性良好且普遍存在时,我们可以抛弃自动化垃圾.


en4*_*4bz 6

我在使用 flex 时也遇到了这个问题。

我用-std=gnu99而不是-std=c99哪个解决了问题。

flex lang.l && gcc -o lexer -std=gnu99 lex.yy.c -lfl                         
Run Code Online (Sandbox Code Playgroud)


Aru*_*run 5

考虑添加以下行:

extern char *strdup(const char *s);
Run Code Online (Sandbox Code Playgroud)

我编译时遇到了问题-std=c99 -pedantic -pedantic-errors.添加上面的代码就解决了我的问题.


Luc*_*ore -1

在使用该函数之前先声明该函数:

//declare the function
int storeLexeme();

//use the function here
Run Code Online (Sandbox Code Playgroud)

或包含声明函数的标头。

C 隐式假定未声明的函数具有返回类型int,并根据调用函数的方式推导出参数。这在 C++ 中已被弃用。