尝试编译时出现 QT 未定义引用错误

发布于 2024-11-01 17:22:52 字数 2397 浏览 1 评论 0原文

我在 QT 项目中添加了一个 IcecastServer 类,将头文件添加到 pro 文件中,并添加了一些代码。每次编译都会出现以下错误:

release/icecastserver.o:icecastserver.cpp:(.text+0x39): 对 _imp___ZN10QTcpServerC1EP7QObject' 的未定义引用 release/icecastserver.o:icecastserver.cpp:(.text+0x50):未定义引用impZN12QHostAddressC1ENS_14SpecialAddressE' release/icecastserver.o:icecastserver.cpp:(.text+0x68): 对 _imp___ZN10QTcpServer6listenERK12QHostAddresst' 的未定义引用 release/icecastserver.o:icecastserver.cpp:(.text+0x73): 未定义引用_imp_ZN12QHostAddressD1Ev' release/icecastserver.o:icecastserver.cpp:(.text+0x9d): 对 _imp___ZNK10QTcpServer11errorStringEv' 的未定义引用 release/icecastserver.o:icecastserver.cpp:(.text+0x3d4): 未定义引用impZN12QHostAddressD1Ev' release/icecastserver.o:icecastserver.cpp:(.text+0x4bd): 对 _imp___ZN10QTcpServerC1EP7QObject' 的未定义引用 release/icecastserver.o:icecastserver.cpp:(.text+0x4d4):未定义引用_imp_ZN12QHostAddressC1ENS_14SpecialAddressE' release/icecastserver.o:icecastserver.cpp:(.text+0x4ec): 对 _imp___ZN10QTcpServer6listenERK12QHostAddresst' 的未定义引用 release/icecastserver.o:icecastserver.cpp:(.text+0x4f7): 未定义引用impZN12QHostAddressD1Ev' release/icecastserver.o:icecastserver.cpp:(.text+0x521): 对 _imp___ZNK10QTcpServer11errorStringEv' 的未定义引用 release/icecastserver.o:icecastserver.cpp:(.text+0x858):未定义引用_imp_ZN12QHostAddressD1Ev'

我做错了什么?

这是头文件:

#ifndef ICECASTSERVER_H
#define ICECASTSERVER_H

#include <QObject>

QT_BEGIN_NAMESPACE
class QTcpServer;
QT_END_NAMESPACE

class IcecastServer : public QObject
{
    Q_OBJECT
public:
    explicit IcecastServer(QObject *parent = 0);

signals:

public slots:

private:
    QTcpServer *tcpServer;
};

#endif // ICECASTSERVER_H

这是源文件:

#include "icecastserver.h"
#include "QDebug"
#include <QtNetwork/QTcpServer>
#include <QtGui>

IcecastServer::IcecastServer(QObject *parent) :
    QObject(parent)
{

    tcpServer = new QTcpServer(this);
    //tcpServer->listen(QHostAddress::Any,8000);

    if (!tcpServer->listen()){
        QMessageBox::critical(NULL, tr("Fortune Server"), tr("Unable to start the server: %1.").arg(tcpServer->errorString()));

         return;
    }

}

I added a class IcecastServer to my QT-project, added the header-file to the pro file and added some code. Everytime I compile it the following errors occur:

release/icecastserver.o:icecastserver.cpp:(.text+0x39): undefined reference to _imp___ZN10QTcpServerC1EP7QObject'
release/icecastserver.o:icecastserver.cpp:(.text+0x50): undefined reference to
impZN12QHostAddressC1ENS_14SpecialAddressE'
release/icecastserver.o:icecastserver.cpp:(.text+0x68): undefined reference to _imp___ZN10QTcpServer6listenERK12QHostAddresst'
release/icecastserver.o:icecastserver.cpp:(.text+0x73): undefined reference to
_imp
_ZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x9d): undefined reference to _imp___ZNK10QTcpServer11errorStringEv'
release/icecastserver.o:icecastserver.cpp:(.text+0x3d4): undefined reference to
impZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x4bd): undefined reference to _imp___ZN10QTcpServerC1EP7QObject'
release/icecastserver.o:icecastserver.cpp:(.text+0x4d4): undefined reference to
_imp
_ZN12QHostAddressC1ENS_14SpecialAddressE'
release/icecastserver.o:icecastserver.cpp:(.text+0x4ec): undefined reference to _imp___ZN10QTcpServer6listenERK12QHostAddresst'
release/icecastserver.o:icecastserver.cpp:(.text+0x4f7): undefined reference to
impZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x521): undefined reference to _imp___ZNK10QTcpServer11errorStringEv'
release/icecastserver.o:icecastserver.cpp:(.text+0x858): undefined reference to
_imp
_ZN12QHostAddressD1Ev'

What am I doing wrong?

This is the header-file:

#ifndef ICECASTSERVER_H
#define ICECASTSERVER_H

#include <QObject>

QT_BEGIN_NAMESPACE
class QTcpServer;
QT_END_NAMESPACE

class IcecastServer : public QObject
{
    Q_OBJECT
public:
    explicit IcecastServer(QObject *parent = 0);

signals:

public slots:

private:
    QTcpServer *tcpServer;
};

#endif // ICECASTSERVER_H

This is the source-file:

#include "icecastserver.h"
#include "QDebug"
#include <QtNetwork/QTcpServer>
#include <QtGui>

IcecastServer::IcecastServer(QObject *parent) :
    QObject(parent)
{

    tcpServer = new QTcpServer(this);
    //tcpServer->listen(QHostAddress::Any,8000);

    if (!tcpServer->listen()){
        QMessageBox::critical(NULL, tr("Fortune Server"), tr("Unable to start the server: %1.").arg(tcpServer->errorString()));

         return;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2024-11-08 17:22:52

首先,您需要在某处#include 假设注释掉的行是导致问题的原因。

您还可以检查一些项目设置,看看您是否拥有所有正确的输入。

编辑:更详细的

QtNetwork 需要 QT 网络库输入。再次假设您正在使用 QtCreator,因为没有提供太多信息,那么这意味着在您的 .pro 文件中您需要有这样一行:

QT += network

然后确保包含正确的标头在使用它们之前先了解您正在使用的对象。如果您仍然遇到未定义的引用、链接错误等,请尝试 QMake 并重建。如果它仍然存在,除了 QT 使用错误之外,您的代码中可能还存在错误,并且应该验证您所使用的方法和对象是否已正确声明。

最终编辑:很高兴成功...

当您在 QTCreator 中创建新项目时,向导中有一个步骤,您可以在其中勾选要包含的各种 QT 库,这将为您添加这些 .pro 行和输入。这是输入附加库文件的 QT 版本,我相信默认情况下它们将被静态链接。如果您想动态链接共享对象或 dll,则需要一些额外的配置步骤。

First you need to #include <QHostAddress> somewhere assuming that commented out line is what caused the problem.

You may also to check some project settings to see if you have all the correct inputs.

Edit: More detail

QtNetwork requires QT network lib inputs. Assuming once again that you are using QtCreator as not much info was provided then this means in your .pro file you need to have a line like so:

QT += network

Then make sure you include the right headers for the objects you are using before using them. If you still get undefined references, linking errors, etc try a QMake and rebuild. If it still persists you probably have errors in your code in addition to QT usage errors and should validate that your methods and objects being used were properly declared.

Final Edit: Glad that worked ...

When you create a new project in QTCreator there is a step in the wizard where you can check off the various QT libraries you want to include which will add these .pro lines and inputs for you. This is QT version of inputting additional lib files and by default they will be statically linked I believe. If you want to dynamically link with shared objects or dll then there is some extra configuration steps.

不知所踪 2024-11-08 17:22:52

您是否在头文件上运行 moc 工具?您随后是否编译 moc 工具的输出?

Are you running the moc tool over your header file? Are you subsequently compiling the output from the moc tool?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文