0

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})
asked Jan 17, 2024 at 13:44
10
  • 1
    It seems to have nothing to do with QT. You could as well ask "how to use CMake for multi-component project". Read about add_library, target_include_directories, target_link_libraries. Some example: github.com/ttroy50/cmake-examples/tree/master/02-sub-projects Commented Jan 17, 2024 at 15:53
  • 1
    And never #include "../../whatever.h" Commented Jan 17, 2024 at 15:54
  • 1
    @pptaszni It's a Qt5 project, and the file contains several Qt specific lines, so Qt tag is appropriate. Commented Jan 17, 2024 at 16:02
  • 1
    It looks like ../../dockwidgets/dockwidget.h path is provided by you. So again, target_include_directories, organize and link your targets correctly, then provide good include path, like #include "dockwidgets/dockwidget.h". Can't tell more, because question is incomplete (e.g. missing CMakeLists from subdirs). Also don't post pictures of code, in QT *.ui are normal text files (sometimes big, so you can cut irrelevant parts). Commented Jan 18, 2024 at 6:52
  • 1
    @pptaszni sorry you are right ../../dockwidgets/dockwidget.h path was provided of my collegue. I changed to ui/components/dockwidgets/dockwidget.h and all works Commented Jan 18, 2024 at 7:40

1 Answer 1

0

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.

answered Jan 19, 2024 at 6:49
Sign up to request clarification or add additional context in comments.

1 Comment

target_include_directories() must be after add_executable()

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.