12
votes

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?

6
I changed the flex tag, as it's about Adobe flex and not the GNU lex clone.Some programmer dude
What platform, UNIX or Windows?paxdiablo
Those are not 'libraries'; they are 'headers'. Libraries are used in the link line; the source code references headers.Jonathan Leffler

6 Answers

11
votes

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.

9
votes

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.

6
votes

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
votes

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.

0
votes

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

0
votes

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