helppls
helppls

Reputation: 61

Why am I getting "error: implicit declaration of function ‘fileno’" when I try to compile on Linux but not on a Mac

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.

Upvotes: 4

Views: 5000

Answers (3)

NVS Abhilash
NVS Abhilash

Reputation: 577

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

Upvotes: -1

n. m. could be an AI
n. m. could be an AI

Reputation: 120011

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.

Upvotes: 11

AnT stands with Russia
AnT stands with Russia

Reputation: 320631

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.

Upvotes: 4

Related Questions