error: ‘errno’ was not declared in this scope


問題:

將一個c文件改為cpp文件,其中的perror()改用C++中的std::cerr << strerror(error) << std::endl;來替換。

重新編譯文件,出現錯誤:

david@ubuntu:~/wrk/tmp/cpp_src/sysapps$ make
g++ -g3 -Wall -o0 -c message_recv.cpp -o message_recv.o
message_recv.cpp: In constructor ‘Message_recv::Message_recv()’:
message_recv.cpp:59:45: error: ‘errno’ was not declared in this scope
message_recv.cpp:66:47: error: ‘errno’ was not declared in this scope
message_recv.cpp: In destructor ‘virtual Message_recv::~Message_recv()’:
message_recv.cpp:76:47: error: ‘errno’ was not declared in this scope
message_recv.cpp: In member function ‘virtual void* Message_recv::run()’:
message_recv.cpp:87:51: error: ‘errno’ was not declared in this scope
make: *** [message_recv.o] Error 1
david@ubuntu:~/wrk/tmp/cpp_src/sysapps$

解決辦法:

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

所以,重新編譯時,將<stdio.h>替換為<string.h>。

2. errno無疑是一個系統定義的全局變量,不是由用戶來聲明或定義的。

查到<errno.h>中有這樣的宏定義

#ifndef errno
extern int errno;
#endif
將頭文件<errno.h>包含到程序中,重新編譯,編譯順利完成。


問題解決。




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM