Eurydice
Eurydice

Reputation: 8449

cmake 2.8.12 and qt5 error at link

I would like to build with cmake (2.8.12) a basic qt5 project generated for testing purpose from qtcreator. The project is just a QApplication that contains a main dialog with a single push button. I followed the instructions given on qt.org and some previous stack discussion to finally come up with the following cmake file:

cmake_minimum_required(VERSION 2.8.11)

project(test)

set(CMAKE_AUTOMOC ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Widgets REQUIRED)

add_executable(test WIN32 main.cpp)

target_link_libraries(test Qt5::Widgets)

The build runs fine up to the link stage where I get the following error:

main.cpp:(.text+0x36): undefined reference to `MainWindow::MainWindow(QWidget*)'
main.cpp:(.text+0x55): undefined reference to `MainWindow::~MainWindow()'
main.cpp:(.text+0x74): undefined reference to `MainWindow::~MainWindow()'

As far as I understood from qt.org instructions, I do not think that I missed any step in setting my cmake file. Would you have any idea of what is wrong with that code ?

EDIT:

Here is finally how Imade my cmake file work:

cmake_minimum_required(VERSION 2.8.11)

project(test)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Widgets REQUIRED)

qt5_wrap_ui(test_ui mainwindow.ui)

add_executable(test WIN32 main.cpp mainwindow.cpp ${test_ui})
target_link_libraries(test Qt5::Widgets)

thanks

Upvotes: 0

Views: 365

Answers (1)

spellmansamnesty
spellmansamnesty

Reputation: 479

You're missing the source file for MainWindow, likely held in your local file named MainWindow.cpp. Did the example use a QMainWindow? This was probably missed because now you are using a custom subclass of QMainWindow.

Modify the cmake line to:

add_executable(test WIN32 main.cpp MainWindow.cpp)

Upvotes: 1

Related Questions