Unable to compile, ui header never found. (Qt6, Clion, Windows)
-
Hello, recently tried to convert my shell app into a GUI app with Qt, but had some problems at build time.
(This app is a model kit collection manager and its name is MonkeyModelKit, thus the names of the files you'll see down there...)Currently I get this error:
C:/Users/Albert/Documents/COALAs/monkey-model-kit/src/gui/MonkeyWindow.cpp:9:10: fatal error: ui_MonkeyWindow.h: No such file or directory 9 | #include "ui_MonkeyWindow.h" | ^~~~~~~~~~~~~~~~~~~
Here's what my project directory tree looks like more or less (only kept the code files and removed resources):
The Cmakelists looks like this:
cmake_minimum_required(VERSION 3.28) project(MonkeyModelKit LANGUAGES CXX) set(CMAKE_PREFIX_PATH "C:/Qt/6.6.0/mingw_64") set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/gui) find_package(Qt6 COMPONENTS Core Gui Widgets REQUIRED) set(SOURCES src/main.cpp src/StringManipulation.cpp src/run/MonkeyShell.cpp src/run/MonkeyManager.cpp src/run/MonkeyFile.cpp src/col/MonkeyModel.cpp src/col/MonkeySession.cpp src/col/MonkeyCollection.cpp src/gui/MonkeyWindow.cpp # Add more source files as needed ) set(UI_FILES gui/MonkeyWindow.ui # Add more UI files as needed ) qt6_wrap_ui(UI_HEADERS ${UI_FILES}) include_directories( include/ include/col include/run include/gui /gui ) add_executable(MonkeyModelKit WIN32 ${SOURCES} ${UI_FILES} ${UI_HEADERS} ) target_include_directories(MonkeyModelKit PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/gui ) target_link_libraries(MonkeyModelKit PRIVATE Qt6::Widgets)
And the .cpp class:
#include "MonkeyWindow.hpp" #include "ui_MonkeyWindow.h" MonkeyWindow::MonkeyWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MonkeyWindow) { ui->setupUi(this); } MonkeyWindow::~MonkeyWindow() { delete ui; } #include "MonkeyWindow.hpp" #include "ui_MonkeyWindow.h" MonkeyWindow::MonkeyWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MonkeyWindow) { ui->setupUi(this); } MonkeyWindow::~MonkeyWindow() { delete ui; }
I don't really know what to do right now, its been a few weeks that I simply can not build my project and can not start learning how Qt works at all...
The only way to make it work is to have all the MonkeyWindow files (.cpp, .hpp and .ui) in the same subdirectory and then everything works all fine. I saw somewhere that the new cpp standard says to not separate header files and source files but I find this super messy, is this right (technically would fix my problem but would make working on the proj so hard) ?
Thanks for the help ...
-
Hi and welcome to devnet,
Looking at the autouic documentation, it think you either have to add
gui/
in front ofui_MonkeyWindow.h
of your include or add gui to the autouic search paths. -
Nevermind, thanks a lot for your help, I was about to ask you if I wasn't including gui to the autouic search paths already with this
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_CURRENT_SOURCE_DIR}/gui)
But in fact what im doing is adding /src/gui/ as the autouic search paths ... so unless you can tell me how to include /gui/ ill have to keep the .ui and .cpp file in the same folder.
Also, now that it does find the ui header, it says that there's this error:
undefined reference to `vtable for MonkeyWindow'
If I got it correctly its because I forgot to implement a virtual function ? What could I be missing in the .cpp ?
-
@GAlbert said in Unable to compile, ui header never found. (Qt6, Clion, Windows):
qt6_wrap_ui(UI_HEADERS ${UI_FILES})
Why? Either use autouic or do it manually. Remove this.
What could I be missing in the .cpp ?
Maybe the Q_OBJECT macro in the header.
-
Why? Either use autouic or do it manually. Remove this.
Yup, didn't know that, learnt it yesterday and fixed my "header not found" problem by using ${CMAKE_CURRENT_SOURCE_DIR}/gui as a autouic parameter.
Maybe the Q_OBJECT macro in the header.
Nah, I already have the Q_OBJECT macro in the header :/
Here it is:#include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MonkeyWindow; } QT_END_NAMESPACE class MonkeyWindow : public QMainWindow { Q_OBJECT public: explicit MonkeyWindow(QWidget *parent = nullptr); ~MonkeyWindow() override; private: Ui::MonkeyWindow *ui; };
-
Nuke the build folder and start again, it might be stale from all the tests you did to get uic sorted out.
-
@SGaist Just did, still got hit with the
C:\Users\Albert\AppData\Local\Programs\CLion\bin\mingw\bin/ld.exe: CMakeFiles/MonkeyModelKit.dir/src/gui/MonkeyWindow.cpp.obj:MonkeyWindow.c:(.rdata$.refptr._ZTV12MonkeyWindow[.refptr._ZTV12MonkeyWindow]+0x0): undefined reference to `vtable for MonkeyWindow' collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed.
😢
By the way, is there a way for me to get exactly what this refptr is that is not defined ? I assume if I find what ".refptr._ZTV12MonkeyWindow]+0x0" is I might be able to define it ? Or am I too C based pointer pilled ?
-
Where is your header file located? Normally AUTOMOC should parse your header and generate & compile the corresponding moc_MonkeyWindow.cpp
Please add#include "moc_MonkeyWindow.cpp"
at the end of your MonkeyWindow.cpp to see if this fixes the problem. Or add MonkeyWindow.hpp to yourSOURCES
to see if this helps (it should according the sources). -
@Christian-Ehrlicher uhhhm... I think we got our culprit, I ain't finding no "moc_MonkeyWindow.cpp" file in the build dir... the closest file name I could find would be "mocs_compilation.cpp"
-
Alright, it looks like the moc file can not be generated if the only way I add headers is by using :
include_directories( include/ include/col include/run include/gui gui/ )
What I in fact need to do is :
set(HEADERS include/gui/MonkeyWindow.hpp )
And manually add all the headers... Unless someone knows how to fix this ill have to add all the headers manually ...
-
I've solve this problem by copying Qt Creator and docs' CMake configuration.
First do include ui_*.h file in the beginning, ignoring error from IDEs:// in my project, the mainwindow.cpp and mainwindow.ui are in same folder #include "./ui_mainwindow.h"
Then add CMake options like this, and run cmake.
-DCMAKE_PREFIX_PATH=C:\Qt\6.7.2\mingw_64\lib\cmake -DCMAKE_GENERATOR:STRING=Ninja -DQT_QMAKE_EXECUTABLE:FILEPATH=C:/Qt/6.7.2/mingw_64/bin/qmake.exe -DCMAKE_C_COMPILER:FILEPATH=C:/Qt/Tools/mingw1120_64/bin/gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:/Qt/Tools/mingw1120_64/bin/g++.exe -DCMAKE_CXX_FLAGS_INIT:STRING=-DQT_QML_DEBUG # Remember to substitute with real path
Finally, run this command:
cmake --build <your-build-path> --target all
the key is the "--target all" option.
you can add this option in CLion by Preference -> CMake -> Cache Variables -> Build options. -
Also, for different file path of .ui file, both change CMakeLists.txt:
set(PROJECT_SOURCES main.cpp mainwindow.cpp mainwindow.h Form/mainwindow.ui )
and the mainwindow.cpp's include path:
#include "./Form/ui_mainwindow.h"