cerrnogcc8

function declaration isn’t a prototype


The following code compiles fine:

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

extern int errno ;

int main ( void )
{
    FILE *fp;
    int errnum;
    fp = fopen ("testFile.txt", "rb");

    if ( fp == NULL )
    {
        errnum = errno;
        fprintf( stderr, "Value of errno: %d\n", errno );
        perror( "Error printed by perror" );
        fprintf( stderr, "Error opening file: %s\n", strerror( errnum ) );
        exit( 1 );
    }

    fclose ( fp );
}

But I can not compile it with:

gcc-8 -Wall -Wextra -Werror -Wstrict-prototypes

I get the following:

 program.c:6:1: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
 extern int errno ;
 ^~~~~~
cc1: all warnings being treated as errors

How can I avoid/fix this? I need this compiler flag -Wstrict-prototypes


Solution

  • extern int errno ;
    

    is wrong. You should simply remove this line.

    What's going on is that you're including <errno.h>, which defines a macro called errno. Well, actually ...

    It is unspecified whether errno is a macro or an identifier declared with external linkage. If a macro definition is suppressed in order to access an actual object, or a program defines an identifier with the name errno, the behavior is undefined.

    (That's from C99, 7.5 Errors <errno.h>.)

    In your case errno probably expands to something like (*__errno()), so your declaration becomes

    extern int (*__errno());
    

    which declares __errno as a function (with an unspecified parameter list) returning a pointer to int.