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'