error: ‘errno’ was not declared in this scope

190 篇文章 80 订阅 ¥29.90 ¥99.00
在将C文件转换为CPP文件并用std::cerr替换perror()后,编译时遇到'errno'未声明的错误。解决方法包括:1. 将<stdio.h>头文件替换为<string.h>,因为strerror()仅需要<string.h>;2. 注意errno是系统定义的全局变量,不需要用户声明。通过修改头文件和理解errno的定义,成功解决问题。
摘要由CSDN通过智能技术生成



转自http://blog.csdn.net/david_xtd/article/details/9151373




问题:

将一个c文件改为cpp文件,其中的perror()改用C++中的std::cerr << strerror(error) << std::endl;来替换。

重新编译文件,出现错误:

[plain]  view plain copy print ?
  1. david@ubuntu:~/wrk/tmp/cpp_src/sysapps$ make  
  2. g++ -g3 -Wall -o0 -c message_recv.cpp -o message_recv.o  
  3. message_recv.cpp: In constructor ‘Message_recv::Message_recv()’:  
  4. message_recv.cpp:59:45: error: ‘errno’ was not declared in this scope  
  5. message_recv.cpp:66:47: error: ‘errno’ was not declared in this scope  
  6. message_recv.cpp: In destructor ‘virtual Message_recv::~Message_recv()’:  
  7. message_recv.cpp:76:47: error: ‘errno’ was not declared in this scope  
  8. message_recv.cpp: In member function ‘virtual void* Message_recv::run()’:  
  9. message_recv.cpp:87:51: error: ‘errno’ was not declared in this scope  
  10. make: *** [message_recv.o] Error 1  
  11. david@ubuntu:~/wrk/tmp/cpp_src/sysapps$  

解决办法:

1. 原来的perror()需要用到头文件<stdio.h>,而strerror()只需要用到头文件<string.h>,

所以,重新编译时,将<stdio.h>替换为<string.h>。

2. errno无疑是一个系统定义的全局变量,不是由用户来声明或定义的。

查到<errno.h>中有这样的宏定义

[plain]  view plain copy print ?
  1. #ifndef errno  
  2. extern int errno;  
  3. #endif  
将头文件<errno.h>包含到程序中,重新编译,编译顺利完成。


问题解决。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值