Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 函数声明isn’;这不是一个原型_C_Errno_Gcc8 - Fatal编程技术网

C 函数声明isn’;这不是一个原型

C 函数声明isn’;这不是一个原型,c,errno,gcc8,C,Errno,Gcc8,以下代码编译得很好: #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 )

以下代码编译得很好:

#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 );
}
我得到以下信息:

 program.c:6:1: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
 extern int errno ;
 ^~~~~~
cc1: all warnings being treated as errors
如何避免/修复此问题?我需要这个编译器标志
-Wstrict prototype

extern int errno ;
这是错误的。您只需删除这一行即可

现在要做的是包括
,它定义了一个名为
errno
的宏。嗯,实际上

未指定
errno
是否为 宏或使用外部链接声明的标识符。如果宏定义被抑制 为了访问一个实际对象,或一个程序定义了一个带有名称的标识符
errno
,行为未定义

(来自C99,7.5错误

在您的情况下,
errno
可能会扩展为类似于
(*\uu errno())
,因此您的声明变得

extern int (*__errno());

它将
\uuu errno
声明为一个函数(带有未指定的参数列表),返回指向
int

的指针。您永远不应该自己声明
errno
。它不一定是一个变量(它可能是一个调用函数的宏,这是很常见的)。您是对的,手册上说:不要这样做==>
在一些古老的系统上,不存在或没有声明errno,因此有必要手动声明errno(即extern int errno)。不要这样做。它在很久以前就不再是必需的了,它会给C库的现代版本带来问题。
如果你愿意,你可以将它添加到你的答案中。顺便说一下,你是对的,这编译起来很好==>
extern(*int_errno(void))(我不会使用它),这使你的答案是正确的。
extern int (*__errno());