简体   繁体   中英

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

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?

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.

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.

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.

I also had this problem while using flex.

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

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

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++.

just place your function below the library calls it will be alright;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM