I am still a newb in CMake trying to get the hang of things. I am trying to build my Qt application using CMake it works fine when the CMakeLists.txt and the project files are in the same directory, but as soon as I split everything into subdirectories it doesn’t seem to work.
My current directory structure is
ProjectRoot/
├── CMakeLists.txt
├── include/
│ └── mainwindow.h
└── src/
├── main.cpp
└── mainwindow.cpp
This is my current CMakeLists.txt
cmake_minimum_required(VERSION 3.29.0)
project(helloworld VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
set(QT5_DIR "C:/Qt/5.15.1/mingw81_64/")
set(CMAKE_PREFIX_PATH ${QT5_DIR})
find_package(Qt5 COMPONENTS Widgets Core Gui REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_executable(helloworld
src/mainwindow.cpp
src/main.cpp
)
target_link_libraries(helloworld Qt5::Widgets Qt5::Core Qt5::Gui)
And this is the error I am getting
0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/helloworld.dir/objects.a(mainwindow.cpp.obj):mainwindow.cpp:(.rdata$.refptr._ZTV10MainWindow[.refptr._ZTV10MainWindow]+0x0): undefined reference to `vtable for MainWindow'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/helloworld.dir/build.make:138: helloworld.exe] Error 1
make[1]: *** [CMakeFiles/Makefile2:86: CMakeFiles/helloworld.dir/all] Error 2
make: *** [Makefile:91: all] Error 2