How to compile single cpp without header file in Qt ?
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
Code:
#-------------------------------------------------
#
# Project created by QtCreator 2010-12-14T21:46:25
#
#-------------------------------------------------
QT += gui
TARGET = toggle
CONFIG += app_bundle
TEMPLATE = app
SOURCES += main.cpp
toggle.cpp
Code:
#include <QtGui/QApplication>
#include <QDialog>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
#include <QMessageBox>
{
Q_OBJECT;
public:
private slots:
void buttonClicked();
void buttonToggled();
private:
};
{
toggleButton->setCheckable( true );
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 )
{
toggle *dlg = new toggle();
dlg->show();
return app.exec();
}
Re: How to compile single cpp without header file in Qt ?
Hi,
Did you run qmake ?
Regards,
Marc
Re: How to compile single cpp without header file in Qt ?
You need to moc the cpp file, try adding '#include "main.moc' and then running qmake and rebuilding.
Re: How to compile single cpp without header file in Qt ?
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.
Re: How to compile single cpp without header file in Qt ?
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