0

I have a C++ project using Qt with CMAKE that is organized in the following folders:

├── app
│   ├── CMakeLists.txt
│   └── main.cpp
├── CMakeLists.txt
├── include
│   └── MainWindow
│       └── MainWindow.h
└── src
    ├── CMakeLists.txt
    ├── MainWindow.cpp
    └── MainWindow.ui


In the different CMakeLists.txt I have the following information:

CMakeLists.txt

cmake_minimum_required(VERSION 3.23)
project(prj VERSION 0.0.1 LANGUAGES CXX)
add_subdirectory(app)
add_subdirectory(src)

app/CMakeLists.txt

add_executable(app main.cpp)
target_link_libraries(app PRIVATE MainWindow)

src/CMakeLists.txt

# Configure AUTO
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
# Generar librería para la interfaz gráfica.
add_library(MainWindow MainWindow.cpp MainWindow.ui)
target_include_directories(MainWindow PUBLIC ../include)
target_compile_features(MainWindow PUBLIC cxx_std_14)
# Load QT files
set(CMAKE_PREFIX_PATH "/opt/Qt/5.15.2/gcc_64/lib/cmake")
find_package(Qt5 COMPONENTS Core Gui Widgets REQUIRED)
target_link_libraries(MainWindow Qt5::Core Qt5::Gui Qt5::Widgets)

What modifications or additions do I have to make in the different CMake files so that with a similar folder distribution I can get the application to work correctly without having errors?

The exact error, after all the modificications, is the following one:

FAILED: app/app 
: && /usr/bin/c++ -g  app/CMakeFiles/app.dir/main.cpp.o -o app/app  -Wl,-rpath,/opt/Qt/5.15.2/gcc_64/lib  src/libMainWindow.a  /opt/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5.15.2  /opt/Qt/5.15.2/gcc_64/lib/libQt5Gui.so.5.15.2  /opt/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5.15.2 && :
/usr/bin/ld: src/libMainWindow.a(MainWindow.cpp.o): in function `MainWindow::MainWindow(QWidget*)':
/home/developer/CLionProjects/qtTest_v0/src/MainWindow.cpp:12: undefined reference to `vtable for MainWindow'
/usr/bin/ld: /home/developer/CLionProjects/qtTest_v0/src/MainWindow.cpp:12: undefined reference to `vtable for MainWindow'
/usr/bin/ld: src/libMainWindow.a(MainWindow.cpp.o): in function `MainWindow::~MainWindow()':
/home/developer/CLionProjects/qtTest_v0/src/MainWindow.cpp:16: undefined reference to `vtable for MainWindow'
/usr/bin/ld: /home/developer/CLionProjects/qtTest_v0/src/MainWindow.cpp:16: undefined reference to `vtable for MainWindow'
  • "... without having many errors" - Which exact errors you are talking about? Add them to the question post. (In case of many errors make sure to add at least the very first one.) – Tsyvarev Sep 30 '22 at 10:41
  • @Tsyvarev yes sorry, I forgot to put the errors. I have already modified the post. – Alejandro Fernández Sep 30 '22 at 10:54
  • 1
    It seems you expect your `.ui` to be **automatically** processed, but forget to set [CMAKE_AUTOMOC](https://cmake.org/cmake/help/latest/variable/CMAKE_AUTOMOC.html) variable. – Tsyvarev Sep 30 '22 at 11:12
  • @Tsyvarev I have added that variables in **src/CMakeLists.txt** but the problem is still not solved – Alejandro Fernández Sep 30 '22 at 11:19
  • Please, edit the code in the question post to **actual** one, which sets `CMAKE_AUTOMOC` variable. Also add the **exact** error message, as it is shown by the compiler. – Tsyvarev Sep 30 '22 at 11:43
  • For the `.ui` files you should add them as additional source to your `add_library` command and set `CMAKE_AUTOUIC` variable too. – vre Sep 30 '22 at 11:45
  • @Tsyvarev I have update all the information with the exact error and trying to simplify the way I put the documents in the different folders – Alejandro Fernández Sep 30 '22 at 18:52
  • The variables like `CMAKE_AUTOMOC` should be set **before** creating a library/executable target. Otherwise (like in your case) that setting doesn't affect on the target. – Tsyvarev Sep 30 '22 at 19:20
  • @Tsyvarev ok, thank you so much for all your help. Now that error is not present and I have to solve the next one.. **undefined reference to `vtable for MainWindow'**. I have seen that I can solve it with: ```add_library(MainWindow MainWindow.cpp MainWindow.ui ../include/MainWindow/MainWindow.h)``` in the src/CMakeLists.txt, but I think that it's not the best way.. – Alejandro Fernández Sep 30 '22 at 21:02
  • Have you checked other questions about the same error message in QT project? According to [some of them](https://stackoverflow.com/a/14010962/3440745) the problem could be with C++ code. So you need to provide that code as well. – Tsyvarev Sep 30 '22 at 21:15
  • @Tsyvarev I can provide the code if you want but it's the basic template that Qt creates when you make the combo MainWindow.cpp MainWindow.h and MainWindow.ui. As far as I could check the error seems to be that in the folder where I have the .h I have no CMakeLists.txt and it seems that the Qt library is not being loaded correctly, because if I move the MainWindow.h file to the src folder the error is removed. – Alejandro Fernández Sep 30 '22 at 21:18

0 Answers0