声明“errno”时出现编译时错误

Compile-time error on declaration of `errno`

本文关键字:编译时错误 errno 声明      更新时间:2023-10-16

在Linux上编译我的C++程序的过程中,它给出了以下警告:

warning #584: omission of exception specification is incompatible with previous function "__errno_location" (declared at line 43 of "/usr/include/bits/errno.h")
extern int errno;//error handling
         ^

代码如下所示:

#include <errno.h>    //for error handling
#include <cmath>
#include <cstring>
#include <ctime>
extern int errno;//error handling

errno是全局变量,则在其他.cpp文件中使用。 我该如何解决这个问题?

errno必须是"可修改的左值",不一定是声明的对象。

显然,在您的实现中,errno扩展到一个表达式,其中包含对名为 __errno_location 的函数的调用。

在您自己的声明中:

extern int errno;

errno将展开到该表达式,从而导致错误。

既然<errno.h>已经为你声明了errno,就没有必要自己声明,而且如你所见,你不能自己声明。

只是省略extern声明。