Home > database >  Can't build Qt-Widgets Application using CMake
Can't build Qt-Widgets Application using CMake

Time:06-14

I am trying to setup a CMake Project building a Qt-Widgets application but can't compile it properly. My project structure is as follows:

  • include/
    • mainwindow.hpp
  • resources/
    • mainwindow.ui
  • src/
    • main.cpp
    • mainwindow.cpp
  • CMakeLists.txt

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
find_package(Qt5 COMPONENTS Widgets REQUIRED)

project(Test VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS resources)

add_executable(App src/mainwindow.cpp src/main.cpp resources/mainwindow.ui)
target_include_directories(App PRIVATE include)
target_link_libraries(App PRIVATE Qt5::Widgets)

mainwindow.hpp

#ifndef MAINWINDOW_HPP_
#define MAINWINDOW_HPP_

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class Test : public QMainWindow {
    Q_OBJECT

public:
    Test(QWidget *parent = nullptr);
    ~Test();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_HPP_

mainwindow.cpp

#include "mainwindow.hpp"
#include "ui_mainwindow.h"

Test::Test(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){
    ui->setupUi(this);
}

Test::~Test(){
    delete ui;
}

main.cpp

#include "mainwindow.hpp"
#include <QApplication>


int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    Test w;
    w.show();
    return a.exec();
}

When I'm trying to build the project:

cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug
cmake --build build --target all

I get the following linker error:

[ 20%] Automatic MOC and UIC for target App
[ 20%] Built target App_autogen
[ 40%] Building CXX object CMakeFiles/App.dir/App_autogen/mocs_compilation.cpp.o
[ 60%] Building CXX object CMakeFiles/App.dir/src/mainwindow.cpp.o
[ 80%] Building CXX object CMakeFiles/App.dir/src/main.cpp.o
[100%] Linking CXX executable App
/usr/bin/ld: CMakeFiles/App.dir/src/mainwindow.cpp.o: in function `Test::Test(QWidget*)': .../src/mainwindow.cpp:4: undefined reference to `vtable for Test'
/usr/bin/ld: .../src/mainwindow.cpp:4: undefined reference to `vtable for Test'
/usr/bin/ld: CMakeFiles/App.dir/src/mainwindow.cpp.o: in function `Test::~Test()': .../src/mainwindow.cpp:8: undefined reference to `vtable for Test'
/usr/bin/ld: .../src/mainwindow.cpp:8: undefined reference to `vtable for Test'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/App.dir/build.make:132: App] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/App.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Looking at cmakes generated link.txt, it seems that the required object files are linked properly

/usr/bin/c   \
    -g CMakeFiles/App.dir/App_autogen/mocs_compilation.cpp.o \
       CMakeFiles/App.dir/src/mainwindow.cpp.o \
       CMakeFiles/App.dir/src/main.cpp.o \
    -o App \
       /usr/lib/libQt5Widgets.so.5.15.4 \
       /usr/lib/libQt5Gui.so.5.15.4 \
       /usr/lib/libQt5Core.so.5.15.4 

Im not really sure, why the vtable references are undefined. What am I doing wrong here?

CodePudding user response:

The solution to this is, adding include/mainwindow.hpp to the sources.

add_executable(App src/mainwindow.cpp src/main.cpp include/mainwindow.hpp resources/mainwindow.ui)

This seems to be required for the generated ui_mainwindow.h to see the vtable entries of the MainWindow class.

  • Related