gpt4 book ai didi

c - 如何删除以下 'implicit declaration of function' 警告?

转载 作者:太空狗 更新时间:2023-10-29 16:39:46 24 4
gpt4 key购买 nike

如何使用 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'

这些是我包含的库。

%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
%}

函数 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);
}

如何删除警告?

最佳答案

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

现在它们是否在您的平台上可用取决于您的平台。


如果您使用的是 Microsoft 工具,您可能需要查看 _fileno 对于后者(VC2005 中的 fileno was deprecated)。 strdup 的一个相当优秀的版本可以查到here .

尽管我已经用那个代码吹响了我自己的喇叭,你也可以使用 _strdup 因为它取代了 also-deprecated strdup :-)

这些应该可以正常工作,因为它们在 stdio.h 中和 string.h ,您已经在使用的两个包含文件。


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

一种可能性是,如果您在一种严格模式下编译,例如 __STRICT_ANSI__在 gcc 中),其中两者都不会被定义。

您应该查看生成的 lex.yy.c 的顶部和 lex.l文件以确认包含头文件,并检查您传递给编译器的命令行参数。

关于c - 如何删除以下 'implicit declaration of function' 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com