You are not logged in.
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
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
Add #include guards to header.hpp
Offline
Add #include guards to header.hpp
Include guards made nothing. Still the same error.
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
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? This is C++ code...
Last edited by stqn (2010-09-11 09:58:02)
Offline
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