user17780852
user17780852

Reputation:

Building a simple qt project with CMake

I'm trying to build a simple qt project with CMake, which contains main.cxx, MainWindow.cxx and MainWindow.hxx. When I try to make install it says fatal error: 'QMainWindow' file not found, but I do added Widgets in the CMakelists.txt. Here are the codes: main.cxx:

#include <QApplication>

#include "MainWindow.h"

int main(int argc, char** argv){
        QApplication GUI(argc, argv);
        MainWindow window;
        window.shou();

        return GUI.exec();
}

MainWindow.cxx


MainWindow::MainWindow(QWidget *parent) 
     : QMainWindow(parent)
{

}

MainWindow::~MainWindow()
{

}

MainWindow.hxx

#ifndef MAINWINDOW_HXX_
#define MAINWINDOW_HXX_

#include <QMainWindow>

class MainWindow : public QMainWindow
{
        Q_OBJECT

public:
      MainWindow(QWidget *parent =0);
      ~MainWindow();
};

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(Gui_Window)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)


find_package(Qt6 COMPONENTS Widgets REQUIRED)


add_library(MAINWINDOW ${CMAKE_CURRENT_SOURCE_DIR}/src/MainWindow.cxx)

add_executable(Gui_Window ${CMAKE_CURRENT_SOURCE_DIR}/app/main.cxx)

target_link_libraries(Gui_Window PUBLIC Qt6::Widgets
                                        MAINWINDOW
                     )

install(TARGETS Gui_Window DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/bin)

What should be the problem?

Upvotes: 0

Views: 1123

Answers (1)

mzimmers
mzimmers

Reputation: 902

Your MainWindow.cxx file should be included in your CMakeLists.txt file like this:

set(PROJECT_SOURCES MainWindow.cxx)

You'd want main in there too.

As an aside, it's much easier to use Qt Creator and let it generate the CMakeLists.txt file for you.

Upvotes: 1

Related Questions