函数声明不是原型
function declaration isn’t a prototype
以下代码可以正常编译:
#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 );
}
但是我不能编译它:
gcc-8 -Wall -Wextra -Werror -Wstrict-prototypes
我得到以下信息:
program.c:6:1: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
extern int errno ;
^~~~~~
cc1: all warnings being treated as errors
我怎么能avoid/fix这个?我需要这个编译器标志 -Wstrict-prototypes
extern int errno ;
错了。你应该简单地删除这一行。
发生的事情是您包含了 <errno.h>
,它定义了一个名为 errno
的宏。嗯,其实...
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.
(来自 C99,7.5 个错误 <errno.h>
。)
在你的情况下 errno
可能会扩展为类似 (*__errno())
的东西,所以你的声明变成
extern int (*__errno());
将 __errno
声明为函数(具有未指定的参数列表)返回指向 int
.
的指针
以下代码可以正常编译:
#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 );
}
但是我不能编译它:
gcc-8 -Wall -Wextra -Werror -Wstrict-prototypes
我得到以下信息:
program.c:6:1: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
extern int errno ;
^~~~~~
cc1: all warnings being treated as errors
我怎么能avoid/fix这个?我需要这个编译器标志 -Wstrict-prototypes
extern int errno ;
错了。你应该简单地删除这一行。
发生的事情是您包含了 <errno.h>
,它定义了一个名为 errno
的宏。嗯,其实...
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 nameerrno
, the behavior is undefined.
(来自 C99,7.5 个错误 <errno.h>
。)
在你的情况下 errno
可能会扩展为类似 (*__errno())
的东西,所以你的声明变成
extern int (*__errno());
将 __errno
声明为函数(具有未指定的参数列表)返回指向 int
.