【问题标题】:Qt Socket problemsQt 套接字问题
【发布时间】:2012-01-11 16:38:08
【问题描述】:

我不知道为什么会出现这些错误。我正在使用 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 时遇到了许多令人沮丧的问题...我似乎永远无法找到正确添加库的方法,而且我尝试的每一项新事物都需要永远开始工作...我错过了什么吗??

请回答以下问题:

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

【问题讨论】:

  • 解决了我的问题!谢谢!

标签: c++ qt sockets linker


【解决方案1】:

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

QT       += core gui network

【讨论】:

  • 非常感谢...我真的很喜欢 Qt,但是我缺少一些关于如何正确使用不同库并将它们合并到我的项目中的基本知识。有没有什么材料可以指点我,以便我更好地自我教育?
  • @PTBG:使用 Qt 时,我发现 Qt documentation quite helpful 就足够了。一开始看起来很多,但在逐步了解基础知识后,您将更好地了解在哪里深入研究。
  • 我将开始通读。非常感谢您的帮助,再次感谢!
猜你喜欢
  • 2017-04-15
  • 2011-01-03
  • 2010-12-19
  • 2011-06-17
  • 2011-08-11
  • 2017-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多