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());