PDA

View Full Version : Compiling QT4 Code using cmake 2.6



Serenity
8th November 2008, 15:47
Hi Guys!

I have a big Problem. I want to compile my Source Code with Cmake, because later I must add another lib and that isn't possible with qmake.

First here the Code:
widget.h


#define _WIDGET_H_

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QtGui>


class MyWidget : public QWidget{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
private:
QLineEdit *lineEdit_v;
QTableWidget *tablewidget;
QLineEdit *lineEdit_l;
QLineEdit *lineEdit_ru;
int richt[3][200];
int counter;
//QLabel *bla;
//QPushButton *subsampling;
//QLabel *fertig;

public slots:

void copy_list();
};



widget.cpp:


#include "widget.h"
#include <QtGui>
#include <QHeaderView>
#include <QTableWidget>

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{

//Attribute
counter=0;
lineEdit_v = new QLineEdit();
lineEdit_l = new QLineEdit();
lineEdit_ru = new QLineEdit();

QPushButton *send = new QPushButton("Ab in die Liste");

tablewidget = new QTableWidget(30, 3, this);

QLabel *label_l = new QLabel(tr("Drehung:"));
QLabel *label_ru = new QLabel(tr("Dauer:"));
QLabel *label_v = new QLabel(tr("Speed:"));

//Attributen-Zuweisungen
lineEdit_v->setMaximumSize( 210, lineEdit_v->maximumSize().height() );
lineEdit_l->setMaximumSize( 210, lineEdit_l->maximumSize().height() );
lineEdit_ru->setMaximumSize( 210, lineEdit_ru->maximumSize().height() );

//Anpassung des QTableWidgets
//
//Ueberschrift
QStringList fonts;
fonts << "Speed" << "Drehung" << "Dauer";
tablewidget->setHorizontalHeaderLabels(fonts);

//SIGNALS AND SLOTS

connect(send, SIGNAL(clicked()),this , SLOT(copy_list()));

//LAYOUT

QVBoxLayout *drehung = new QVBoxLayout();
drehung->addWidget(label_l);
drehung->addWidget(lineEdit_l);

QVBoxLayout *vor = new QVBoxLayout();
vor->addWidget(label_v);
vor->addWidget(lineEdit_v);

QVBoxLayout *dauer = new QVBoxLayout();
dauer->addWidget(label_ru);
dauer->addWidget(lineEdit_ru);

QVBoxLayout *linkespalte = new QVBoxLayout();
linkespalte->addLayout(vor);
linkespalte ->addLayout(drehung);
linkespalte->addLayout(dauer);
linkespalte->addWidget(send);

QVBoxLayout *center = new QVBoxLayout();
center->addLayout(linkespalte);
center->addStretch();

QHBoxLayout *end = new QHBoxLayout();
end->addLayout(center);
end->addWidget(tablewidget);




this->setLayout(end);


}



void MyWidget::copy_list(){


QString speed = lineEdit_v->text();
QString dreh = lineEdit_l->text();
QString dauer = lineEdit_ru->text();

QTableWidgetItem *newItem = new QTableWidgetItem(speed);
QTableWidgetItem *degree = new QTableWidgetItem(dreh);
QTableWidgetItem *time = new QTableWidgetItem(dauer);

tablewidget->setItem(counter, 0, newItem);
tablewidget->setItem(counter, 1, degree);
tablewidget->setItem(counter, 2, time);

counter++;
}

main.cpp


#include "widget.h"
#include <QtGui>
#include <QHeaderView>
#include <QTableWidget>


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



And at last but not least the CMakeLists.txt



cmake_minimum_required(VERSION 2.6)


FIND_PACKAGE(Qt4)
SET(QT_USE_QTXML 1)
INCLUDE(${QT_USE_FILE})
QT4_AUTOMOC(main.cpp widget.cpp widget.h)
ADD_EXECUTABLE(myexe main.cpp widget.cpp)
TARGET_LINK_LIBRARIES(myexe ${QT_LIBRARIES})


I have all the files in a folder. In the console, I am typing:

"cmake-2.6 ."


-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for Q_WS_X11
-- Looking for Q_WS_X11 - found
-- Looking for Q_WS_WIN
-- Looking for Q_WS_WIN - not found.
-- Looking for Q_WS_QWS
-- Looking for Q_WS_QWS - not found.
-- Looking for Q_WS_MAC
-- Looking for Q_WS_MAC - not found.
-- Found Qt-Version 4.4.2
-- Found OpenSSL: /usr/lib/libssl.so
-- Looking for _POSIX_TIMERS
-- Looking for _POSIX_TIMERS - found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/serenity/RoboGui

okay, all fine until now.
then I must make it:

typing:
"make"


Scanning dependencies of target myexe
[ 50%] Building CXX object CMakeFiles/myexe.dir/main.cpp.o
[100%] Building CXX object CMakeFiles/myexe.dir/widget.cpp.o
Linking CXX executable myexe
CMakeFiles/myexe.dir/main.cpp.o: In function `MyWidget::~MyWidget()':
main.cpp:(.text._ZN8MyWidgetD1Ev[MyWidget::~MyWidget()]+0xf): undefined reference to `vtable for MyWidget'
main.cpp:(.text._ZN8MyWidgetD1Ev[MyWidget::~MyWidget()]+0x19): undefined reference to `vtable for MyWidget'
CMakeFiles/myexe.dir/widget.cpp.o: In function `MyWidget::MyWidget(QWidget*)':
widget.cpp:(.text+0x2ef): undefined reference to `vtable for MyWidget'
widget.cpp:(.text+0x2f9): undefined reference to `vtable for MyWidget'
CMakeFiles/myexe.dir/widget.cpp.o: In function `MyWidget::MyWidget(QWidget*)':
widget.cpp:(.text+0xf1b): undefined reference to `vtable for MyWidget'
CMakeFiles/myexe.dir/widget.cpp.o:widget.cpp:(.text+0xf25): more undefined references to `vtable for MyWidget' follow
CMakeFiles/myexe.dir/widget.cpp.o: In function `MyWidget::tr(char const*, char const*)':
widget.cpp:(.text._ZN8MyWidget2trEPKcS1_[MyWidget::tr(char const*, char const*)]+0x1e): undefined reference to `MyWidget::staticMetaObject'
collect2: ld gab 1 als Ende-Status zurück
make[2]: *** [myexe] Fehler 1
make[1]: *** [CMakeFiles/myexe.dir/all] Fehler 2
make: *** [all] Fehler 2


Where is the problem and why couldn't I compile my program? ("Fehler" means error)

I also testet it with qmake and it works. But I have to do it with cmake.

Greetings
Serenity

mcosta
9th November 2008, 19:04
I don't know the effect of QT4_AUTOMOC call but in CMake docs I've found

macro QT4_AUTOMOC(sourcefile1 sourcefile2 ... )
This macro is still experimental.
It can be used to have moc automatically handled.
So if you have the files foo.h and foo.cpp, and in foo.h a
a class uses the Q_OBJECT macro, moc has to run on it. If you don't
want to use QT4_WRAP_CPP() (which is reliable and mature), you can insert
#include "foo.moc"
in foo.cpp and then give foo.cpp as argument to QT4_AUTOMOC(). This will the
scan all listed files at cmake-time for such included moc files and if it finds
them cause a rule to be generated to run moc at build time on the
accompanying header file foo.h.
If a source file has the SKIP_AUTOMOC property set it will be ignored by this macro.


You have to put

#include "widget.moc"
in the widget.cpp file and remove widget.h as parameter of QT4_AUTOMOC

See http://http://wiki.qtcentre.org/index.php?title=Compiling_Qt4_apps_with_CMake
that suggest the use of QT4_WRAP_CPP instead of QT4_AUTOMOC.

In your widget.h file you have to insert

#ifndef _WIDGET_H_
as first line and

#endif // _WIDGET_H_
as last line of code