How do I remove the following 'implicit declaration of function' warnings?

一个人想着一个人 提交于 2019-12-04 02:56:07

问题


How do I compile the lex file with gcc without receiving the following warnings?

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'

These are the libraries I included.

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

The function yy_init_buffer is not in the file. The following is the function 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);
 }

How do I remove the warnings?


回答1:


Neither strdup nor fileno are ISO C functions, they're part of POSIX.

Now whether they're available on your platform depends on your platform.


If you are using the Microsoft tools, you may want to look into _fileno for the latter (fileno was deprecated in VC2005). A rather excellent version of strdup can be found here.

Although, having blown my own horn with that code, you could also use _strdup since it replaces the also-deprecated strdup :-)

These should hopefully work okay as-is since they're in stdio.h and string.h, two of the include files you're already using.


If you're on a UNIX derivative, those functions should be available in stdio.h (for fileno) and string.h (for strdup). Given that it looks like you're already including those files, the problem is likely elsewhere.

One possibility is if you're compiling in one of the strict modes like __STRICT_ANSI__ in gcc), where neither would be defined.

You should have a look at the top of your generated lex.yy.c and lex.l files to confirm that the header files are being included and also check the command line parameters you're passing to the compiler.




回答2:


I suggest this option (tell the compiler you are using POSIX):

#define _POSIX_C_SOURCE 1

People seem to have tightened up the feature controls in recent years and hopefully when the consistency is good and widespread we can throw away the automake garbage.




回答3:


Consider adding the following line:

extern char *strdup(const char *s);

I faced the problem when I compiled with -std=c99 -pedantic -pedantic-errors. Adding the above line solved the problem for me.




回答4:


I also had this problem while using flex.

I used -std=gnu99rather than -std=c99 which solved the problem.

flex lang.l && gcc -o lexer -std=gnu99 lex.yy.c -lfl                         



回答5:


You declare the function before you use it:

//declare the function
int storeLexeme();

//use the function here

or include the header where the function is declared.

C implicitly assumes undeclared functions have return type int and deduces the parameters from how you call the function. This is deprecated in C++.



来源:https://stackoverflow.com/questions/9427145/how-do-i-remove-the-following-implicit-declaration-of-function-warnings

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!