尝试编译时出现 QT 未定义引用错误
我在 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'
impZN12QHostAddressC1ENS_14SpecialAddressE'
release/icecastserver.o:icecastserver.cpp:(.text+0x50): undefined reference to
release/icecastserver.o:icecastserver.cpp:(.text+0x68): undefined reference to_imp___ZN10QTcpServer6listenERK12QHostAddresst'
_imp_ZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x73): undefined reference to
release/icecastserver.o:icecastserver.cpp:(.text+0x9d): undefined reference to_imp___ZNK10QTcpServer11errorStringEv'
impZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x3d4): undefined reference to
release/icecastserver.o:icecastserver.cpp:(.text+0x4bd): undefined reference to_imp___ZN10QTcpServerC1EP7QObject'
_imp_ZN12QHostAddressC1ENS_14SpecialAddressE'
release/icecastserver.o:icecastserver.cpp:(.text+0x4d4): undefined reference to
release/icecastserver.o:icecastserver.cpp:(.text+0x4ec): undefined reference to_imp___ZN10QTcpServer6listenERK12QHostAddresst'
impZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x4f7): undefined reference to
release/icecastserver.o:icecastserver.cpp:(.text+0x521): undefined reference to_imp___ZNK10QTcpServer11errorStringEv'
_imp_ZN12QHostAddressD1Ev'
release/icecastserver.o:icecastserver.cpp:(.text+0x858): undefined reference to
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要在某处
#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.
您是否在头文件上运行
moc
工具?您随后是否编译moc
工具的输出?Are you running the
moc
tool over your header file? Are you subsequently compiling the output from themoc
tool?