2011-08-12 66 views
1

我嘗試編寫Web服務器。就我而言,它在Windows中很安靜。但我想讓它與Unix兼容。我認爲,異常類的遺傳必定會有問題。 爲了更好地理解,僅僅是重要的部分:GCC 4.5.2連接器問題使用異常時出現問題(C++)

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() {}; 
}; 

那裏你可以SE類聲明的簡短總結。 但現在的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 

,甚至更多....

+0

你用來鏈接對象的命令行是什麼? – Mark

+0

可能必須'#include '並且還要執行'-lstdC++'來鏈接庫。 – arunkumar

+1

用'g ++'而不是'gcc'編譯? –

回答

6

從它看起來像你與gcc鏈接undefined reference to __gxx_personality_v0鏈接的消息來看。您需要將C++應用程序與g++鏈接。或者鏈接gcc並將-lstdc++添加到鏈接器命令行。

0

您還沒有顯示相關的代碼和編譯器調用,但知道std::exception有一個未實現的虛成員函數what(),你預計覆蓋,所以如果你想打電話what()不要去扔裸std::exception小號。

<stdexcept>中的任何派生的特定例外都將執行what()並允許您在構造異常對象時存儲消息。

+2

事實上,有'__gxx_personality_v0'的未定義引用意味着C++運行時沒有鏈接到。 –

+0

@Maxim:這是一個好點子。我加了一點答案。 –