Get rid of warning: implicit declaration of function ‘fileno’ in flex
Asked Answered
P

2

7

I'm trying to get rid of a warning that gcc is throwing when I try to compile my bison and flex files:

: In function ‘yy_init_buffer’:
:1675: warning: implicit declaration of function ‘fileno’

The reason why I want to do this is because I'm trying to submit an assignment to the class I'm taking but I can only submit the "parser.y" and "scanner.l" files and it gets remotely compiled. The problem is: if there's a warning it gets (for some reason) considered an error and because I have no control over the compiler flags I can't make it disappear. I have seen some questions around the Internet with the same problem, but none of the solutions mentioned worked for me.

The compiler uses se following flags:

bison -d -o parser.c parser.y
flex -i -o scanner.c  scanner.l
gcc -std=c99 -pedantic -o test_parser *.c

I'm using a Mac OSX so when I compile it doesn't give me any warning, so I'm guessing it's something unique to linux distributions. Here's the header section of each file I have so you have an idea of what I've tried already:

scanner.l

#define _POSIX_SOURCE 1
//#define _GNU_SOURCE

#include <stdio.h>
#include <stdlib.h>
# include "parser.h"

parser.y

#include <stdio.h>
#include <stdlib.h>

int yylex (void);
void yyerror (char const *);

Would really appreciate any kind of help here.

Progressist answered 14/9, 2017 at 8:12 Comment(0)
S
15

The traditional answer is, I believe, the one listed in the self-answer: manually declare fileno.

My normal practice (and, I think, the most common solution) is to add -D_XOPEN_SOURCE=700 to my gcc flags. (The 700 is larger than necessary for the declaration of fileno but sometimes I use other Posix features.) An alternative would be setting _POSIX_C_SOURCE. _POSIX_SOURCE is deprecated but it still works.

These need to be compiler options rather than #defines in your flex input file because in the generated flex code, #include <stdio.h> is inserted before the user-specified prologue, and feature test macros need to be defined before the first use of any standard library header. (See man feature-test-macros and/or the Posix specification for more information.)

Since that is not possible in your compilation environment, you'll need to use a workaround. As mentioned, one is to manually declare fileno in your prolog.

Another workaround is to avoid the call of fileno by specifying %option never-interactive, which tells flex to generate a scanner which doesn't attempt to modify its behaviour if the input is a terminal. %option always-interactive would also work, but since the scanner will not be used in an interactive environment, it is an unnecessary overhead. These work because fileno is only used in order to call isatty (which is a Posix function in unistd.h) in order to decide whether to activate interactive handling. If you tell flex that the input is always or never interactive, it doesn't require this test. (fileno is also needed if you specify the read option to avoid use of stdio for input. But you don't and you shouldn't.)

I'm a little surprised that the compilation environment for your course does not include an appropriate feature test macro definition in the compilation step. It should do that, and you can pass my advice on to whoever oversees the build tools.

Spindle answered 14/9, 2017 at 15:34 Comment(0)
P
3

The problem was solved (momentarily) by adding to the flex file:

int fileno(FILE *stream);

This is a bad practice but was the only way I could deal with it.

Progressist answered 14/9, 2017 at 8:55 Comment(1)
Bravo for mentioning that it's bad practice.Quar

© 2022 - 2024 — McMap. All rights reserved.