GCC 4.5.2链接器在使用异常(c++)时遇到问题
GCC 4.5.2 Linker gots Problem while using Exceptions (C++)
我试着写一个web服务器。就我而言,它在Windows上运行得非常好。但我想让它也兼容Unix。我认为异常类的遗传一定有问题。为了更好地理解,只需要重要的部分:
server.cpp
#include <exception>
#include <stdexcept>
#ifdef __unix__
#include "UnixSocket.h"
#elif __WIN32__ || _MSC_VER
#include "WinSocket.h"
#endif
#include "HTTPParser.h"
int main(void) {
try {
socket->socketInit(PORT);
}
catch (exception &e) {
cout << endl << "Exception: " << e.what() << endl;
socket->cleanAll();
}
return 0
}
NetInterface.h
class NetInterface : public exception {
private:
public:
virtual void socketInit(const char *port) = 0;
virtual void cleanAll(void) = 0;
virtual void cleanPersCon(void) = 0;
virtual char *akzeptieren(void) = 0;
virtual void empfangen(void) = 0;
virtual void senden(void) = 0;
virtual void *get_in_addr(struct sockaddr *sa) = 0;
virtual string getIncoming(void) = 0;
virtual void setOutcoming(string s) = 0;
virtual ~NetInterface() throw() {};
};
UnixSocket.h
class UnixSocket : virtual public NetInterface {
private:
[...]
public:
UnixSocket(void);
//kill socket connections
void cleanAll(void);
void cleanPersCon(void);
//SysCalls
void socketInit(const char *port);
char *akzeptieren(void);
void empfangen(void);
void senden(void);
//Getter and Setter
string getIncoming(void);
void setOutcoming(string s);
virtual ~UnixSocket() throw() {};
};
HTTPParser.h
class HTTPParser : public exception {
private:
[...]
public:
HTTPParser(NetInterface *_socket, string _path);
void parsePacket(void);
virtual ~HTTPParser() throw() {};
};
在中可以看到类声明的简短摘要。但是现在gcc告诉我这样的东西:
/tmp/cc8DNmKI.o:(.rodata._ZTV10HTTPParser[vtable for HTTPParser]+0x10): undefined reference to `std::exception::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTV10UnixSocket[vtable for UnixSocket]+0x14): undefined reference to `std::exception::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTV10UnixSocket[vtable for UnixSocket]+0x78): undefined reference to `std::exception::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x10): undefined reference to `std::exception::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x14): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x18): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x1c): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x20): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x24): undefined reference to `__cxa_pure_virtual'
/tmp/cc8DNmKI.o:(.rodata._ZTV12NetInterface[vtable for NetInterface]+0x28): more undefined references to `__cxa_pure_virtual' follow
/tmp/cc8DNmKI.o:(.rodata._ZTVSt16invalid_argument[vtable for std::invalid_argument]+0x10): undefined reference to `std::logic_error::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTVSt12domain_error[vtable for std::domain_error]+0x10): undefined reference to `std::logic_error::what() const'
/tmp/cc8DNmKI.o:(.rodata._ZTI10HTTPParser[typeinfo for HTTPParser]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTI10HTTPParser[typeinfo for HTTPParser]+0x8): undefined reference to `typeinfo for std::exception'
/tmp/cc8DNmKI.o:(.rodata._ZTI10UnixSocket[typeinfo for UnixSocket]+0x0): undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTI10UnixSocket[typeinfo for UnixSocket]+0x18): undefined reference to `typeinfo for std::exception'
/tmp/cc8DNmKI.o:(.rodata._ZTI12NetInterface[typeinfo for NetInterface]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTI12NetInterface[typeinfo for NetInterface]+0x8): undefined reference to `typeinfo for std::exception'
/tmp/cc8DNmKI.o:(.rodata._ZTISt16invalid_argument[typeinfo for std::invalid_argument]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTISt16invalid_argument[typeinfo for std::invalid_argument]+0x8): undefined reference to `typeinfo for std::logic_error'
/tmp/cc8DNmKI.o:(.rodata._ZTISt12domain_error[typeinfo for std::domain_error]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/tmp/cc8DNmKI.o:(.rodata._ZTISt12domain_error[typeinfo for std::domain_error]+0x8): undefined reference to `typeinfo for std::logic_error'
/tmp/cc8DNmKI.o:(.eh_frame+0xeb): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
甚至更多....
从undefined reference to __gxx_personality_v0
链接器消息来看,您似乎正在与gcc
链接。您需要将c++应用程序与g++
链接。或者与gcc
链接,并将-lstdc++
添加到链接器命令行。
您没有显示相关代码和编译器调用,但知道std::exception
有一个未实现的虚拟成员函数what()
,您希望覆盖它,所以如果您想调用what()
,请不要抛出裸std::exception
。
<stdexcept>
中的任何派生的特定异常都将实现what()
,并允许您在构造异常对象时存储消息。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Pytorch torch.cholesky忽略异常