2012-01-11 107 views
2

我無法弄清楚爲什麼我會收到這些錯誤。我正在使用Qt 4.7.4。我正在嘗試使用套接字進行通信的客戶端/服務器程序。我有winsock2庫的工作代碼(在Visual Studio 2008中工作)。此代碼拒絕與Qt合作,我嘗試了幾天。我不斷收到鏈接錯誤,所以我決定嘗試從頭開始使用QTcpSocket。我在.pro文件中包含了所有必需的包含路徑,並且我收到了鏈接器錯誤。Qt套接字問題

這是我的客戶端類:

toplevelcomms.h

#ifndef TOPLEVELCOMMS_H 
#define TOPLEVELCOMMS_H 

#include <QObject> 
#include "inireader.h" 
#include "WinSock2.h" 
#include <QDialog> 
#include <qtcpsocket.h> 
#include <qhostaddress.h> 


/** This class sends/receives information through sockets. 
* 
*/ 

class TopLevelComms : public QObject 
{ 
    Q_OBJECT 
public: 
    TopLevelComms(QString hostIP, quint16 hostPort); 
    void connect(); 

private: 
    QTcpSocket *tcpSocket; 
    QHostAddress hostAddress; 
    quint16 hostPort; 


}; 

#endif // TOPLEVELCOMMS_H 

toplevelcomms.cpp

#include "toplevelcomms.h" 
#include "stdio.h" 

TopLevelComms::TopLevelComms(QString hostIP, quint16 hostPort) 
{ 
    tcpSocket = new QTcpSocket(); 
    hostAddress.setAddress(hostIP); 
    this->hostPort = hostPort; 

} 


void TopLevelComms::connect(){ 
    //tcpSocket->connectToHost(); 
} 

這是.pro文件

include($${PWD}/../qwtbuild.pri) 
include($${PWD}/../qwtconfig.pri) 

QT  += core gui 

TARGET = TSLSuite 
TEMPLATE = app 

... // *all my files are declared here* 


CONFIG += qwt 

INCLUDEPATH += $${PWD}/../include 
INCLUDEPATH += C:/QtSDK/Desktop/Qt/4.7.4/msvc2008/include/Qt 
INCLUDEPATH += C:/QtSDK/Simulator/Qt/msvc2008/bin 
INCLUDEPATH += C:/QtSDK/Simulator/Qt/msvc2008/include/QtNetwork 

LIBS += -LC:/Qwt-6.0.1/lib -lqwt 
LIBS += -LC:/Qt/4.7.4/lib -lQtSvg 

這是輸出:

11:33:57: Running build steps for project TSLSuite... 
11:33:57: Configuration unchanged, skipping qmake step. 
11:33:57: Starting: "C:\QtSDK\QtCreator\bin\jom.exe" 
    C:\QtSDK\QtCreator\bin\jom.exe -nologo -j 2 -f Makefile.Release all 
linking release\TSLSuite.exe 
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::~QHostAddress(void)" ([email protected]@[email protected]) referenced in function [email protected]@[email protected]@@[email protected]$0 
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall QHostAddress::setAddress(class QString const &)" ([email protected]@@[email protected]@@Z) referenced in function "public: __thiscall TopLevelComms::TopLevelComms(class QString,unsigned short)" ([email protected]@[email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QTcpSocket::QTcpSocket(class QObject *)" ([email protected]@[email protected]@@@Z) referenced in function "public: __thiscall TopLevelComms::TopLevelComms(class QString,unsigned short)" ([email protected]@[email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::QHostAddress(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall TopLevelComms::TopLevelComms(class QString,unsigned short)" ([email protected]@[email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall QTcpSocket::metaObject(void)const " ([email protected]@@[email protected]@XZ) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall QTcpSocket::qt_metacast(char const *)" ([email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall QTcpSocket::qt_metacall(enum QMetaObject::Call,int,void * *)" ([email protected]@@[email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::isSequential(void)const " ([email protected]@@UBE_NXZ) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall QAbstractSocket::close(void)" ([email protected]@@UAEXXZ) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::atEnd(void)const " ([email protected]@@UBE_NXZ) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual __int64 __thiscall QAbstractSocket::bytesAvailable(void)const " ([email protected]@@UBE_JXZ) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual __int64 __thiscall QAbstractSocket::bytesToWrite(void)const " ([email protected]@@UBE_JXZ) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::canReadLine(void)const " ([email protected]@@UBE_NXZ) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::waitForReadyRead(int)" ([email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::waitForBytesWritten(int)" ([email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "protected: virtual __int64 __thiscall QAbstractSocket::readData(char *,__int64)" ([email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "protected: virtual __int64 __thiscall QAbstractSocket::readLineData(char *,__int64)" ([email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2001: unresolved external symbol "protected: virtual __int64 __thiscall QAbstractSocket::writeData(char const *,__int64)" ([email protected]@@[email protected]) 
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QTcpSocket::~QTcpSocket(void)" ([email protected]@[email protected]) referenced in function "public: virtual void * __thiscall QTcpSocket::`scalar deleting destructor'(unsigned int)" ([email protected]@[email protected]) 
release\TSLSuite.exe : fatal error LNK1120: 19 unresolved externals 

jom 1.0.6 - empower your cores 

command failed with exit code 1120 
command failed with exit code 2 
11:33:58: The process "C:\QtSDK\QtCreator\bin\jom.exe" exited with code 2. 
Error while building project TSLSuite (target: Desktop) 
When executing build step 'Make' 

我已經得到了Qt許多令人沮喪的問題......我永遠不能似乎找到了如何正確添加庫,每一個新的東西我嘗試永遠需要工作...我錯過了什麼?

請回答以下問題:

  1. 什麼是錯我當前的代碼,以及如何解決它?
  2. 我在做什麼錯誤的Qt?爲什麼我很難使用新的庫和類?
+0

解決我的問題!謝謝! – Aman 2013-05-09 07:47:11

回答

5

在你.pro文件,你需要添加網絡模塊:

QT  += core gui network 
+0

非常感謝您......我非常喜歡Qt,但是我錯過了一些關於如何正確使用不同庫的基本知識,並將它們合併到我的項目中。是否有一些材料可以指向我,這樣我可以更好地教育自己? – PTBG 2012-01-11 16:43:34

+0

@PTBG:當使用Qt時,我發現[Qt文檔相當有用](http://developer.qt.nokia.com/doc/qt-4.8/)並且足夠了。它起初看起來很多,但是在逐步瞭解基礎知識之後,您可以更好地瞭解哪裏可以深入探索。 – 2012-01-11 16:47:07

+0

我會開始閱讀。我非常感謝你的幫助,非常感謝! – PTBG 2012-01-11 16:52:24