As the title states, I am getting the following error: "implicit declaration of function ‘fileno’" when I try to compile on Linux but not on a Mac. I know I can fix it with the simple line int fileno(FILE *stream);, but I want to know why this happening.
3 Answers
You should not declare functions provided by a system library. You should properly include an appropriate header.
Since fileno is not a standard C function, it is not normally declared by in <stdio.h> . You have to define a suitable macro in order to enable the declaration.
man fileno
Requirements for glibc (see feature_test_macros(7)):
fileno(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
Defining any of the the three macros before inclusion of should do the trick.
1 Comment
If you remembered to include <stdio.h>, this warning indicates that your <stdio.h> does not declare this function.
fileno is not a standard function. It is perfectly expected that some implementations do not provide it, especially if you are compiling in strict standard-compliant mode.
If your code still links successfully, this almost certainly means that your Linux compiler settings direct your compiler to work in "strict" mode and thus force <stdio.h> to "hide" declaration of fileno.
Comments
I wrote a simple program in my Ubuntu 16.04 machine. And it compiles well.
#include <stdio.h>
int main ()
{
printf ("%d\n", fileno(stdout));
return 0;
}
I hope you know that fileno is present in <stdio.h>.
If you have not included the header file, please do so and re-compile.
I am not so sure why Mac version works.
EDIT:
I have not used C99 standard.
I compiled using: gcc -Wall -Werror test.c
-std=c11or-std=c99? Try-std=gnu11or-std=gnu99instead.#define _XOPEN_SOURCE 700or#define _POSIX_C_SOURCE 200809Lin effect before the first (POSIX) standard header is included. If you specify-std=c11, then those functions are not declared by defaul.