Search code examples
cgccgcc-warningc11gcc5.2

Implicit declaration of function ‘fgetc_unlocked’, using gcc 5.2


I have the following code:

#include <stdio.h>

int main() {
    putc_unlocked('a', stdout);
    return 0;
}

I get no error when I compile it using gcc file.c, however, if I use gcc -std=c11 file.c, I get:

file.c: In function ‘main’:
file.c:4:2: warning: implicit declaration of function ‘putc_unlocked’ [-Wimplicit-function-declaration]
  putc_unlocked('a', stdout);
  ^

Why?


Solution

  • Compiling using -std=cxx where xx is 99 or 11 depending on what version of C you are using will use different header files than compiling with -std=gnuxx (where again xx = 99 or 11).

    The default setting (if you don't specify a command line argument) for GCC 5.2 is for -std=gnu11.

    The gnu settings define the macros:

    1. _GNU_SOURCE, which turns on GNU only features;
    2. _POSIX_SOURCE, which turns on POSIX features;
    3. and maybe others? (_BSD_SOURCE is a possibility but I'm not sure).

    If you compile with -std=cxx then you get standard C and not any extensions.

    So this warning is because that function is not part of the C standard. Thus you get an implicit declaration of the function (which was allowed by the old C standards and kept for backwards compatibility).

    You can edit your file to have #define _POSIX_SOURCE if you want to compile with -std=cxx.