4

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.

8
  • 1
    And where is the code which shows this problem? Commented Jun 19, 2017 at 5:32
  • 1
    Related: How do I remove the following 'implicit declaration of function' warnings? Commented Jun 19, 2017 at 5:32
  • 2
    What compilation options are you using? -std=c11 or -std=c99? Try -std=gnu11 or -std=gnu99 instead. Commented Jun 19, 2017 at 5:35
  • 5
    Officially, you have to request POSIX functions by having #define _XOPEN_SOURCE 700 or #define _POSIX_C_SOURCE 200809L in effect before the first (POSIX) standard header is included. If you specify -std=c11, then those functions are not declared by defaul. Commented Jun 19, 2017 at 5:57
  • 1
    "I don't know what ISO C and POSIX have to do with Linux and Mac or what they are" You probably want to learn these things if you plan to produce meaningful C code for these systems. Commented Jun 19, 2017 at 6:13

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

1. man.archlinux.org/man/fileno.3 2. man.archlinux.org/man/feature_test_macros.7.en #define _POSIX_C_SOURCE 200809L exposes definitions corresponding to the POSIX.1-2008
4

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

-1

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

3 Comments

Yes I have included <stdio.h>
You are using wrong compiler flags (e.g. none at all).
If you don't use the -std=c99 flag, then there's no error because pre-99 C allowed functions without prototypes first and also had int as the default return value so it all works out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.