Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT undefined reference errors when trying to compile

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 toimpZN12QHostAddressC1ENS_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 toimpZN12QHostAddressD1Ev' 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 toimpZN12QHostAddressD1Ev' 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;
    }

}
like image 450
Hedge Avatar asked Apr 13 '11 23:04

Hedge


2 Answers

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.

like image 184
AJG85 Avatar answered Oct 12 '22 01:10

AJG85


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

like image 43
Thomi Avatar answered Oct 12 '22 01:10

Thomi