Results 1 to 5 of 5

Thread: How to compile single cpp without header file in Qt ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Location
    Jakarta, Indonesia
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2010-12-14T21:46:25
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += gui
    8.  
    9. TARGET = toggle
    10. CONFIG += app_bundle
    11.  
    12. TEMPLATE = app
    13.  
    14.  
    15. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 



    toggle.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QDialog>
    3. #include <QWidget>
    4. #include <QPushButton>
    5. #include <QHBoxLayout>
    6. #include <QMessageBox>
    7.  
    8.  
    9. class toggle : public QDialog
    10. {
    11. Q_OBJECT;
    12.  
    13. public:
    14. toggle( QWidget *parent=0 );
    15.  
    16. private slots:
    17. void buttonClicked();
    18. void buttonToggled();
    19.  
    20. private:
    21. QPushButton *clickButton;
    22. QPushButton *toggleButton;
    23. };
    24.  
    25. toggle::toggle( QWidget *parent ) : QDialog( parent )
    26. {
    27. clickButton = new QPushButton( "Click me!", this );
    28. toggleButton = new QPushButton( "Toggle me!", this );
    29. toggleButton->setCheckable( true );
    30. QHBoxLayout *layout = new QHBoxLayout( this );
    31. layout->addWidget( clickButton );
    32. layout->addWidget( toggleButton );
    33.  
    34. connect( clickButton, SIGNAL(clicked()), this, SLOT(buttonClicked()) );
    35. connect( toggleButton, SIGNAL(clicked()), this, SLOT(buttonToggled()) );
    36.  
    37. };
    38.  
    39. void toggle::buttonClicked()
    40. {
    41. QMessageBox::information( this, "Clicked!", "The button was clicked!" );
    42. };
    43.  
    44. void toggle::buttonToggled()
    45. {
    46. QMessageBox::information( this, "Toggled!", QString("The button is %1!").arg(toggleButton->isChecked()?"pressed":"released") );
    47. }
    48.  
    49.  
    50. int main( int argc, char **argv )
    51. {
    52. QApplication app( argc, argv );
    53.  
    54. toggle *dlg = new toggle();
    55. dlg->show();
    56. return app.exec();
    57. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 14th December 2010 at 23:11. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to compile single cpp without header file in Qt ?

    Hi,

    Did you run qmake ?

    Regards,
    Marc

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  5. #5
    Join Date
    Nov 2010
    Location
    Jakarta, Indonesia
    Posts
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

Similar Threads

  1. Replies: 1
    Last Post: 1st June 2010, 08:05
  2. Single Log File for Qt Project.
    By qtlinuxnewbie in forum Newbie
    Replies: 5
    Last Post: 16th February 2010, 13:00
  3. compile to arm+include header
    By nataly in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2009, 12:59
  4. Multiple appearances in a single ui file
    By zgulser in forum Qt Tools
    Replies: 1
    Last Post: 5th June 2009, 19:12
  5. delete a single file from a directory
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 12th March 2009, 05:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.