Cannot find generated ui header files with CMake and AUTOUIC
-
Hi! I have created a sample GUI application using CMake - standard project setup. I have then created a subdirectory in the root of the project with a library that also has .ui files. Unfortunately after setting it up with CMake, Qt Creator is complaining that it cannot find the generated ui.h file. Any chance that AUTOUIC does not add the correct directory from the build output to my target include directories? The TrialProject creates a widget that belongs to the library - this is when the error occurs.
Here's the CMake code (excluded the irrelevant standard GUI project code):
CMakeLists.txt of the root project
# Standard CMake code with MainWindow class, etc. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/MyLibrary) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/MyLibrary) target_link_libraries(TrialProject PRIVATE Qt${QT_VERSION_MAJOR}::Widgets MyLibrary)
CMakeLists.txt of the library with UI
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOUIC ON) find_package(Qt6 REQUIRED COMPONENTS Core Widgets ) qt_add_library(MyLibrary STATIC ${CMAKE_CURRENT_SOURCE_DIR}/MyLibrary.h ${CMAKE_CURRENT_SOURCE_DIR}/MyLibrary.cpp ${CMAKE_CURRENT_SOURCE_DIR}/MyLibrary.ui ) target_include_directories(MyLibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
Interestingly, when I add the following line to the target_include_directories, the project builds fine, but I am not confident with this solution:
${CMAKE_CURRENT_BINARY_DIR}/MyLibrary_autogen/include
The TrialProject does not seem to have this issue and is able to find its ui_mainwindow.h. I have tried using the qt_wrap_ui() instead, but without luck. I am using MSVC on Windows, if that is relevant to the question.
-
This sounds like https://gitlab.kitware.com/cmake/cmake/-/issues/21846 but I cannot tell for sure without a full example project and the exact error message.
-
When you create a library you should not include generated files in your public headers which you do. Otherwise you have to supply generated code with your library.
Forward-declare the needed classes and include the generated stuff in your sources. -
@Christian-Ehrlicher said in Cannot find generated ui header files with CMake and AUTOUIC:
When you create a library you should not include generated files in your public headers which you do. Otherwise you have to supply generated code with your library.
Forward-declare the needed classes and include the generated stuff in your sources.Forward-declaring the needed classes and including the ui-generated headers only in the .cpp works. I was able to remove the nasty directory from my target_include_directories. However, I have seem people doing this as a solution: ${CMAKE_CURRENT_BINARY_DIR}/MyLibrary_autogen/include - based on your answer, I understand this brings more trouble than benefit.
@jobor said in Cannot find generated ui header files with CMake and AUTOUIC:
This sounds like https://gitlab.kitware.com/cmake/cmake/-/issues/21846 but I cannot tell for sure without a full example project and the exact error message.
Thanks, this brings a lot of insight and explains what could have been wrong with my code.
-
@szumial said in Cannot find generated ui header files with CMake and AUTOUIC:
I have seem people doing this as a solution: ${CMAKE_CURRENT_BINARY_DIR}/MyLibrary_autogen/include - based on your answer, I understand this brings more trouble than benefit.
Correct - it's bad practice as distributing generated intermediate files is wrong. Esp. since those files depend on a specific Qt version and may not work with newer/older Qt versions.
Please mark this topic as solved then.
-