When copying throws an error

CMakeFiles/qthelloworld.dir/MyDialog.cpp.o: In function `MyDialog::MyDialog(QWidget*)': /home/ilia/CLionProjects/qthelloworld/qthelloworld/MyDialog.cpp:7: undefined reference to `vtable for MyDialog' /home/ilia/CLionProjects/qthelloworld/qthelloworld/MyDialog.cpp:7: undefined reference to `vtable for MyDialog' collect2: error: ld returned 1 exit status CMakeFiles/qthelloworld.dir/build.make:101: recipe for target 'qthelloworld' failed make[3]: *** [qthelloworld] Error 1 CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/qthelloworld.dir/all' failed make[2]: *** [CMakeFiles/qthelloworld.dir/all] Error 2 CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/qthelloworld.dir/rule' failed make[1]: *** [CMakeFiles/qthelloworld.dir/rule] Error 2 Makefile:118: recipe for target 'qthelloworld' failed make: *** [qthelloworld] Error 2 

Trying to create a dialog box that says "Hello World".

Here is the main function

 #include <QApplication> #include "MyDialog.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); MyDialog *dialog = new MyDialog; dialog->show(); return app.exec(); } 

In the original code, there are no backslashes in the import, I inserted it in order to show the imports themselves (mb in them is the case).

MyDialog.h file

 #ifndef QTHELLOWORLD_MYDIALOG_H #define QTHELLOWORLD_MYDIALOG_H #include <QtWidgets/QVBoxLayout> #include <QLabel> #include <QPushButton> #include <QtWidgets/QDialog> class MyDialog: public QDialog { Q_OBJECT public: MyDialog(QWidget *parent = 0); }; #endif //QTHELLOWORLD_MYDIALOG_H 

MyDialog.cpp

 #include "MyDialog.h" MyDialog::MyDialog(QWidget *parent) : QDialog(parent) { QVBoxLayout *layout = new QVBoxLayout(this); QLabel *lable = new QLabel(this); lable->setText("<font color = green> Hello, World! </font>"); QPushButton *button = new QPushButton(this); button->setText("close"); layout->addWidget(lable); layout->addWidget(button); connect(button, SIGNAL(clicked()), this, SLOT(close())); } 

I would be grateful if you say what the error is, why it is not compiled.

  • one
    The question is often asked. Standard answer: restart qmake . - AnT 6:08 pm
  • @AnT, yes, only qmake is not used (if I correctly understand that there is qmake) - SPGC
  • Clean up all garbage, especially moc files and try again - Alexander Chernin

2 answers 2

Everyone who writes qt with cmake spends the first time on this error N time. In order for everything to start with qt, you need to add the following fields to your cmake.

 find_package( Qt5 COMPONENTS Widgets REQUIRED ) aux_source_directory( src GUI_SOURCES ) file( GLOB MOC_HEADERS src/*.hpp ) qt5_wrap_cpp( GUI_MOC ${MOC_HEADERS} ) file( GLOB UIS_FILES ui/*.ui ) qt5_wrap_ui( GUI_UI ${UIS_FILES} ) 

Wrote only for your component for your case components can look in the documentation.

Also do not forget to add to executable:

 add_executable( ${PROJECT_NAME} ${app_flags} ${GUI_SOURCES} ${GUI_MOC} ${UIS_FILES} ) 

In the linker target, you also need to add the components that are used in your code.

 target_link_libraries( ${PROJECT_NAME} Qt5::Widgets Qt5::Gui) 

    See CMakeLists.txt of your project. Perhaps not installed automatic use of the moc generator: set (CMAKE_AUTOMOC ON)

    This option allows you to do without manually determining the location of the generated moc files (as recommended in another answer):

     file( GLOB MOC_HEADERS src/*.hpp ) qt5_wrap_cpp( GUI_MOC ${MOC_HEADERS} ) 

    and without prescribing $ {GUI_MOC} to add_executable

    You can read more here: https://cmake.org/cmake/help/latest/manual/cmake-qt.7.html#manual:cmake-qt(7)

    There is also about similar options for UIC and RCC