PDA

View Full Version : How to compile single cpp without header file in Qt ?



alphazero
14th December 2010, 17:25
Dear All,

When I want to compile below script, it shown error like this :

toggle/main.cpp:26: error: undefined reference to `vtable for toggle'

line 26 : toggle::toggle( QWidget *parent ) : QDialog( parent )

and here compile output

/media/DATA/Qt/toggle/main.cpp:26: undefined reference to `vtable for toggle'
/media/DATA/Qt/toggle/main.cpp:26: undefined reference to `vtable for toggle'
/media/DATA/Qt/toggle/main.cpp:26: undefined reference to `vtable for toggle'
/media/DATA/Qt/toggle/main.cpp:26: undefined reference to `vtable for toggle'
collect2: ld returned 1 exit status
make: Leaving directory `/media/DATA/Qt/toggle'
make: *** [toggle] Error 1
The process "/usr/bin/make" exited with code %2.
Error while building project toggle (target: Desktop)
When executing build step 'Make'

Can I compile script w/o .h file ? If I can, how to solve below problem ?

Thanks

alphazero


File :
toggle.pro


#-------------------------------------------------
#
# Project created by QtCreator 2010-12-14T21:46:25
#
#-------------------------------------------------

QT += gui

TARGET = toggle
CONFIG += app_bundle

TEMPLATE = app


SOURCES += main.cpp




toggle.cpp


#include <QtGui/QApplication>
#include <QDialog>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QMessageBox>

class QPushButton;

class toggle : public QDialog
{
Q_OBJECT;

public:
toggle( QWidget *parent=0 );

private slots:
void buttonClicked();
void buttonToggled();

private:
QPushButton *clickButton;
QPushButton *toggleButton;
};

toggle::toggle( QWidget *parent ) : QDialog( parent )
{
clickButton = new QPushButton( "Click me!", this );
toggleButton = new QPushButton( "Toggle me!", this );
toggleButton->setCheckable( true );
QHBoxLayout *layout = new QHBoxLayout( this );
layout->addWidget( clickButton );
layout->addWidget( toggleButton );

connect( clickButton, SIGNAL(clicked()), this, SLOT(buttonClicked()) );
connect( toggleButton, SIGNAL(clicked()), this, SLOT(buttonToggled()) );

};

void toggle::buttonClicked()
{
QMessageBox::information( this, "Clicked!", "The button was clicked!" );
};

void toggle::buttonToggled()
{
QMessageBox::information( this, "Toggled!", QString("The button is %1!").arg(toggleButton->isChecked()?"pressed":"released") );
}


int main( int argc, char **argv )
{
QApplication app( argc, argv );

toggle *dlg = new toggle();
dlg->show();
return app.exec();
}

marcvanriet
14th December 2010, 17:53
Hi,

Did you run qmake ?

Regards,
Marc

squidge
14th December 2010, 18:12
You need to moc the cpp file, try adding '#include "main.moc' and then running qmake and rebuilding.

ChrisW67
14th December 2010, 20:50
The moc-related include should be after the declaration of class toggle as the generated code requires a complete class definition. I usually put it as the last line of the file in these one-file test program or QTestLib tests.

alphazero
14th December 2010, 23:04
Yesterday, I have run qmake, and I didn't see any main.moc built.
But today, when I add script #include "main.moc" on the last line of main.cpp (main.moc is not exist before), and i try to re-run qmake again.. qcreator create "main.moc" automatically.

thanks.. it's working now..

regards,

alphazero