Qt套接字问题

时间:2012-01-11 16:38:08

标签: c++ qt sockets linker

我无法弄清楚为什么我会收到这些错误。我正在使用Qt 4.7.4。我正在尝试创建一个使用套接字进行通信的客户端/服务器程序。我有winsock2库的工作代码(在visual studio 2008中工作)。这段代码拒绝使用Qt,我试了几天。我一直在收到链接器错误,所以我决定尝试从头开始使用QTcpSocket。我在.pro文件中包含了所有必需的包含路径,并且我收到了链接器错误。

这是我的客户端类:

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)" (__imp_??1QHostAddress@@QAE@XZ) referenced in function __unwindfunclet$??0TopLevelComms@@QAE@VQString@@G@Z$0
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall QHostAddress::setAddress(class QString const &)" (__imp_?setAddress@QHostAddress@@QAE_NABVQString@@@Z) referenced in function "public: __thiscall TopLevelComms::TopLevelComms(class QString,unsigned short)" (??0TopLevelComms@@QAE@VQString@@G@Z)
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QTcpSocket::QTcpSocket(class QObject *)" (__imp_??0QTcpSocket@@QAE@PAVQObject@@@Z) referenced in function "public: __thiscall TopLevelComms::TopLevelComms(class QString,unsigned short)" (??0TopLevelComms@@QAE@VQString@@G@Z)
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QHostAddress::QHostAddress(void)" (__imp_??0QHostAddress@@QAE@XZ) referenced in function "public: __thiscall TopLevelComms::TopLevelComms(class QString,unsigned short)" (??0TopLevelComms@@QAE@VQString@@G@Z)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall QTcpSocket::metaObject(void)const " (?metaObject@QTcpSocket@@UBEPBUQMetaObject@@XZ)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall QTcpSocket::qt_metacast(char const *)" (?qt_metacast@QTcpSocket@@UAEPAXPBD@Z)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall QTcpSocket::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@QTcpSocket@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::isSequential(void)const " (?isSequential@QAbstractSocket@@UBE_NXZ)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall QAbstractSocket::close(void)" (?close@QAbstractSocket@@UAEXXZ)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::atEnd(void)const " (?atEnd@QAbstractSocket@@UBE_NXZ)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual __int64 __thiscall QAbstractSocket::bytesAvailable(void)const " (?bytesAvailable@QAbstractSocket@@UBE_JXZ)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual __int64 __thiscall QAbstractSocket::bytesToWrite(void)const " (?bytesToWrite@QAbstractSocket@@UBE_JXZ)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::canReadLine(void)const " (?canReadLine@QAbstractSocket@@UBE_NXZ)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::waitForReadyRead(int)" (?waitForReadyRead@QAbstractSocket@@UAE_NH@Z)
toplevelcomms.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall QAbstractSocket::waitForBytesWritten(int)" (?waitForBytesWritten@QAbstractSocket@@UAE_NH@Z)
toplevelcomms.obj : error LNK2001: unresolved external symbol "protected: virtual __int64 __thiscall QAbstractSocket::readData(char *,__int64)" (?readData@QAbstractSocket@@MAE_JPAD_J@Z)
toplevelcomms.obj : error LNK2001: unresolved external symbol "protected: virtual __int64 __thiscall QAbstractSocket::readLineData(char *,__int64)" (?readLineData@QAbstractSocket@@MAE_JPAD_J@Z)
toplevelcomms.obj : error LNK2001: unresolved external symbol "protected: virtual __int64 __thiscall QAbstractSocket::writeData(char const *,__int64)" (?writeData@QAbstractSocket@@MAE_JPBD_J@Z)
toplevelcomms.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QTcpSocket::~QTcpSocket(void)" (__imp_??1QTcpSocket@@UAE@XZ) referenced in function "public: virtual void * __thiscall QTcpSocket::`scalar deleting destructor'(unsigned int)" (??_GQTcpSocket@@UAEPAXI@Z)
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中遇到了许多令人沮丧的问题...我似乎永远无法找到如何正确添加库,我尝试的每一件新事物都需要永远工作......我错过了什么?< / p>

请回答以下问题:

  1. 我当前的代码有什么问题,我该如何解决?
  2. Qt我做错了什么?为什么我这么难以使用新的库和类?

1 个答案:

答案 0 :(得分:5)

.pro文件中,您需要添加网络模块:

QT       += core gui network