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

c lex
idealistikz picture idealistikz · Feb 24, 2012 · Viewed 27.9k times · Source

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?

Answer

paxdiablo picture paxdiablo · Feb 24, 2012

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.