0
votes

I am trying to build a simple Qt5 application using CMake The Qt5 project is the basic project generated when creating a new project with a Widget. The project builds and runs successfully with QtCreator widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

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

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

main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

The CMakeLists file have been written according to the example given in the Qt5 documentation. The path to the Qt5 directory is given in the cache. http://doc.qt.io/qt-5/cmake-manual.html

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.11)

project(test0)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets)

# Tell CMake to create the executable
add_executable(test0 WIN32 main.cpp)

# Use the Widgets module from Qt 5.
target_link_libraries(test0 Qt5::Widgets)

The Cmake generation works fine.

I'm getting linking errors (undefined reference to methods belonging to the Widget class) when building the app using the Makfile generated by Cmake. (here is a capture of the errors) http://s31.postimg.org/edefl1m6j/Capturetest0.png

Any tips ?

System : Windows 7

Compiler : MinGW32

Versions : QT 5.6.1 (mingw49_32) CMake 3.6.0

1
Can you give us the compilation line? make clean && make VERBOSE=1Gui13
CMake doesn't guess about widget.cpp file, which defines "undefined" methods. You need to list that file explicitely for add_executable: add_executable(test0 WIN32 main.cpp widget.cpp).Tsyvarev
That's right, it works. I'll be more careful next time, thanks a lot !apazat

1 Answers

0
votes

Two mistakes in the CMakeLists - widget.cpp has to be mentionend in add_executable as explained Tsyvarev - use autouic in order to create the ui_widget.h associated with the widget.ui and widget.cpp

The working CMakeLists is the following :

cmake_minimum_required(VERSION 2.8.11)

project(test0)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)

# Find the QtWidgets library
find_package(Qt5Widgets)

# Tell CMake to create the executable
add_executable(test0 WIN32 main.cpp widget.cpp widget.ui)

# Use the Widgets module from Qt 5.
target_link_libraries(test0 Qt5::Widgets)