You are not logged in.

#1 2010-09-10 12:51:08

Kosmonavt
Member
Registered: 2010-02-15
Posts: 100

Fighting linker errors when building Qt app manually

Hi all,

some time I was using Qt and building apps in IDE (Code::Blocks). Now I need to find a way to build manually - an app must be built on a machine without IDE or anything similar. I followed official manual, but project failed at linking. I'm now using test project, it consists of two files, and still fails:

main.cpp:

#include <QApplication>
#include "header.hpp"

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    TestClass asdf(0);
    asdf.show();
    return app.exec();
}

header.hpp:

#include <QWidget>
#include <QtGui>

class TestClass : public QWidget
{
Q_OBJECT
public:
    TestClass(QWidget* parent);
protected:
    void paintEvent(QPaintEvent* event);
};

TestClass::TestClass(QWidget* parent) : QWidget(parent)
{
    setWindowTitle( tr("Testing window") );
    resize(200, 200);
}
void TestClass::paintEvent(QPaintEvent* event)
{
    QPainter ps(this);
    ps.drawLine(10, 10, 100, 100);
}

Then I ran qmake -project; qmake test1.pro; make. The following error occured:

moc_header.o: In function `TestClass::paintEvent(QPaintEvent*)':
moc_header.cpp:(.text+0x80): multiple definition of `TestClass::paintEvent(QPaintEvent*)'
main.o:main.cpp:(.text+0xc0): first defined here
moc_header.o: In function `TestClass::TestClass(QWidget*)':
moc_header.cpp:(.text+0xf0): multiple definition of `TestClass::TestClass(QWidget*)'
main.o:main.cpp:(.text+0x0): first defined here
moc_header.o: In function `TestClass::TestClass(QWidget*)':
moc_header.cpp:(.text+0xf0): multiple definition of `TestClass::TestClass(QWidget*)'
main.o:main.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [test1] Error 1

It generated the following test1.pro:

######################################################################
# Automatically generated by qmake (2.01a) Fri Sep 10 16:42:10 2010
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += header.hpp
SOURCES += main.cpp

It's quite strange since in Code::Blocks it builds flawlessly. Is it error in code, or configs need to be corrected?

Offline

#2 2010-09-10 19:02:42

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: Fighting linker errors when building Qt app manually

I don't know why it builds in Code::Blocks, but I would do as the error message suggests and move the functions definitions from the header to a cpp file.

Or move them into the class declaration.

Last edited by stqn (2010-09-10 19:03:31)

Offline

#3 2010-09-10 19:19:21

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Fighting linker errors when building Qt app manually

Add #include guards to header.hpp

Offline

#4 2010-09-11 06:46:30

Kosmonavt
Member
Registered: 2010-02-15
Posts: 100

Re: Fighting linker errors when building Qt app manually

falconindy wrote:

Add #include guards to header.hpp

Include guards made nothing. Still the same error.

stqn wrote:

I don't know why it builds in Code::Blocks, but I would do as the error message suggests and move the functions definitions from the header to a cpp file.

Or move them into the class declaration.

It solved the problem, but I simply can't understand, why definitions in header are causing an error. Basically it is correct, since in C99 aren't any rule rejecting it, only a recommendation. E.g., Boost is made the same way - definitions are put in headers.

Offline

#5 2010-09-11 09:56:13

stqn
Member
Registered: 2010-03-19
Posts: 1,191
Website

Re: Fighting linker errors when building Qt app manually

You're including this header from two different .cpp files, so the functions are duplicated in two different object files, and then the linker complains.

If you want the functions to be inline, then you can move them inside the class declaration "class TestClass { ... };". Otherwise, moving them to a .cpp of its own as you did solves the problem.

Edit: I don't understand what C99 is doing here? smile This is C++ code...

Last edited by stqn (2010-09-11 09:58:02)

Offline

#6 2010-09-11 10:35:35

Kosmonavt
Member
Registered: 2010-02-15
Posts: 100

Re: Fighting linker errors when building Qt app manually

Do I understand correctly that Qt generates additional cpp files so class members got duplicated? Because when building console app without dedicated source file for members it works ok.

Offline

Board footer

Powered by FluxBB