How to use CMake for multi-component QT project

95 Views Asked by At

I created project in QT designer. In designer I use my own widget

class DockWidget : public QDockWidget

enter image description here enter image description here But during building in linux server I got an error

dockwidget.h: No such file or directory

because during building created temp folders where contains generated ui_mainwindow.h file

$ cmake --build $BUILD_DIR/$BUILD_TYPE
[  1%] Automatic MOC and UIC for target eda
[  1%] Built target eda_autogen
[  3%] Generating qrc_resources.cpp
[  5%] Generating qrc_qml.cpp
[  7%] Building CXX object CMakeFiles/eda.dir/eda_autogen/mocs_compilation.cpp.o
In file included from /home/gitlab-runner/builds/pNUPGYMp/0/cad/eda/build/Release/eda_autogen/DL2WFWMK6B/../../../../ui/view/mainmenu/mainmenu.h:6,
                 from /home/gitlab-runner/builds/pNUPGYMp/0/cad/eda/build/Release/eda_autogen/DL2WFWMK6B/moc_mainmenu.cpp:10,
                 from /home/gitlab-runner/builds/pNUPGYMp/0/cad/eda/build/Release/eda_autogen/mocs_compilation.cpp:17:
/home/gitlab-runner/builds/pNUPGYMp/0/cad/eda/build/Release/eda_autogen/include/ui/view/mainwindow/ui_mainwindow.h:24:10: fatal error: ../../components/dockwidgets/dockwidget.h: No such file or directory
 #include "../../components/dockwidgets/dockwidget.h"
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

Files dockwidget.h, mainwindow.ui and mainwindow.h located in different folders. My project structure is

ProjectFolder/
|_CMakeLists.txt
|_ui/
  |_CMakeLists.txt
  |_view/
  | |_CMakeLists.txt
  | |_mainwindow/
  |   |_CMakeLists.txt
  |   |_mainwindow.cpp
  |   |_mainwindow.h
  |   |_mainwindow.ui
  |_components/
    |_CMakeLists.txt
    |_dockwidgets/
      |_CMakeLists.txt
      |_dockwidget.h

What is incorrect in my environment?

UPD: My CMakeLists.txt is

cmake_minimum_required(VERSION 3.25)
set(PROJECT "myproj")
project(${PROJECT} LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_SOURCE_DIR})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH})
SET(CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR} ${CMAKE_PREFIX_PATH})

find_package(Qt5 COMPONENTS Widgets UiTools Quick Qml QuickWidgets OpenGL QuickControls2 REQUIRED)
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)

add_subdirectory(entities)
add_subdirectory(models)
add_subdirectory(ui)

qt5_add_resources(RSS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ui/resources/resources.qrc)
qt5_add_resources(RSS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ui/resources/qml.qrc)

add_executable(${PROJECT} ${SOURCE} ${HEADERS} ${RSS_SOURCES})
set_target_properties(${PROJECT} PROPERTIES AUTOMOC ON)
target_link_libraries(${PROJECT} PRIVATE Qt5::Widgets Qt5::UiTools Qt5::Qml Qt5::Quick Qt5::QuickWidgets ${QT_INTEGRATION_PLUGIN})
1

There are 1 best solutions below

1
Hakan Kaya On

The error you're encountering is due to the relative include path in your ui_mainwindow.h file, which cannot find the dockwidget.h file because of the different folder structure. You need to adjust your project's CMakeLists.txt to correctly include the necessary directories in the include path. You can add include directories to your CMakeLists.txt using the target_include_directories command. In your case, you should add the include directory for the components/dockwidgets folder so that the compiler can find dockwidget.h. Here's how you can modify your CMakeLists.txt:

cmake_minimum_required(VERSION 3.25)
set(PROJECT "myproj")
project(${PROJECT} LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_SOURCE_DIR})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH})
SET(CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR} ${CMAKE_PREFIX_PATH})

find_package(Qt5 COMPONENTS Widgets UiTools Quick Qml QuickWidgets OpenGL QuickControls2 REQUIRED)
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)

add_subdirectory(entities)
add_subdirectory(models)
add_subdirectory(ui)



qt5_add_resources(RSS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ui/resources/resources.qrc)
qt5_add_resources(RSS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ui/resources/qml.qrc)

add_executable(${PROJECT} ${SOURCE} ${HEADERS} ${RSS_SOURCES})

# Add include directory for the dockwidgets component
target_include_directories(${PROJECT} PRIVATE ${CMAKE_SOURCE_DIR}/components/dockwidgets)

set_target_properties(${PROJECT} PROPERTIES AUTOMOC ON)
target_link_libraries(${PROJECT} PRIVATE Qt5::Widgets Qt5::UiTools Qt5::Qml Qt5::Quick Qt5::QuickWidgets ${QT_INTEGRATION_PLUGIN})

By adding target_include_directories(${PROJECT} PRIVATE ${CMAKE_SOURCE_DIR}/components/dockwidgets), you're telling CMake to include the components/dockwidgets directory when compiling the myproj target, which should resolve the dockwidget.h include error. Make sure to adjust the path if necessary to match your actual project structure.