CMake File not found Project Sources
-
I am currently running Qt 6.6.3 with QtCreator 12.0.2.
I am trying to understand CMake a bit better.If the directory structure is simple and all header and source files are in the same directory as the main CMakeLists.txt, Qt is able to find the required files during building and clangd finds it for the Qt Creator IDE.
But if I make some changes in the directory structure like maybe having folders called
includes
andsrc
and try to add them into CMakeLists.txt, clangd as well as the build system is not able to find it.CMakeLists.txt:
cmake_minimum_required(VERSION 3.5) project(CMakeLearn VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) # This works # set(PROJECT_SOURCES # main.cpp # mainwindow.cpp # mainwindow.hpp # mainwindow.ui # ) #This does not work set(PROJECT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/includes/mainwindow.hpp ${CMAKE_CURRENT_SOURCE_DIR}/src/mainwindow.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/mainwindow.ui ) # This does not work either # set(PROJECT_SOURCES # src/main.cpp # includes/mainwindow.hpp # src/mainwindow.cpp # src/mainwindow.ui # ) message(STATUS "The files in PROJECT SOURCES are ${PROJECT_SOURCES}") if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(CMakeLearn MANUAL_FINALIZATION ${PROJECT_SOURCES} ) # Define target properties for Android with Qt 6 as: # set_property(TARGET CMakeLearn APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR # ${CMAKE_CURRENT_SOURCE_DIR}/android) # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation else() if(ANDROID) add_library(CMakeLearn SHARED ${PROJECT_SOURCES} ) # Define properties for Android with Qt 5 after find_package() calls as: # set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android") else() add_executable(CMakeLearn ${PROJECT_SOURCES} ) endif() endif() target_link_libraries(CMakeLearn PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) # Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. # If you are developing for iOS or macOS you should consider setting an # explicit, fixed bundle identifier manually though. if(${QT_VERSION} VERSION_LESS 6.1.0) set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.CMakeLearn) endif() set_target_properties(CMakeLearn PROPERTIES ${BUNDLE_ID_OPTION} MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) include(GNUInstallDirs) install(TARGETS CMakeLearn BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) if(QT_VERSION_MAJOR EQUAL 6) qt_finalize_executable(CMakeLearn) endif()
The directory structure is found by CMake. When I run CMake, I do not have any errors. Only during building or trying to work on the files I get errors.
How do I solve this? Thank you
-
@KK2022 said in CMake File not found Project Sources:
Only during building
What errors do you get when you build? Please check in the "Compile Output" tab for build errors, not in the editor.
If CMake finds the files it should work.
You should also do a complete rebuild after changing CMakeLists.txt file. -
@jsulm Thank you for your response.
Under Tab General Messages after running CMake, I get[cmake] Running C:\Program Files\CMake\bin\cmake.exe -S C:/Users/User1/Documents/Qt_Projects/CMakeLearn -B C:/Users/User1/Documents/Qt_Projects/build-CMakeLearn-Desktop_Qt_6_6_3_MinGW_64_bit-Debug in C:\Users\User1\Documents\Qt_Projects\build-CMakeLearn-Desktop_Qt_6_6_3_MinGW_64_bit-Debug. [cmake] -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) [cmake] -- The files in PROJECT SOURCES are C:/Users/User1/Documents/Qt_Projects/CMakeLearn/src/main.cpp;C:/Users/User1/Documents/Qt_Projects/CMakeLearn/includes/mainwindow.hpp;C:/Users/User1/Documents/Qt_Projects/CMakeLearn/src/mainwindow.cpp;C:/Users/User1/Documents/Qt_Projects/CMakeLearn/src/mainwindow.ui [cmake] -- Configuring done (0.6s) [cmake] -- Generating done (0.0s) [cmake] -- Build files have been written to: C:/Users/User1/Documents/Qt_Projects/build-CMakeLearn-Desktop_Qt_6_6_3_MinGW_64_bit-Debug [cmake] [cmake] Elapsed time: 00:01.
Under Tab Compile Output, after trying to Rebuild I get
09:29:17: Running steps for project CMakeLearn... 09:29:17: Starting: "C:\Program Files\CMake\bin\cmake.exe" --build C:/Users/User1/Documents/Qt_Projects/build-CMakeLearn-Desktop_Qt_6_6_3_MinGW_64_bit-Debug --target clean [1/2 9.7/sec] Cleaning additional files... [2/2 15.0/sec] Cleaning all built files... Cleaning... 2 files. 09:29:17: The process "C:\Program Files\CMake\bin\cmake.exe" exited normally. 09:29:17: Starting: "C:\Program Files\CMake\bin\cmake.exe" --build C:/Users/User1/Documents/Qt_Projects/build-CMakeLearn-Desktop_Qt_6_6_3_MinGW_64_bit-Debug --target all [1/5 0.2/sec] Automatic MOC and UIC for target CMakeLearn [2/5 0.3/sec] Building CXX object CMakeFiles/CMakeLearn.dir/src/main.cpp.obj FAILED: CMakeFiles/CMakeLearn.dir/src/main.cpp.obj C:\Qt\Tools\mingw1120_64\bin\g++.exe -DMINGW_HAS_SECURE_API=1 -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NEEDS_QMAIN -DQT_WIDGETS_LIB -DUNICODE -DWIN32 -DWIN64 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -D_UNICODE -D_WIN64 -IC:/Users/User1/Documents/Qt_Projects/build-CMakeLearn-Desktop_Qt_6_6_3_MinGW_64_bit-Debug/CMakeLearn_autogen/include -isystem C:/Qt/6.6.3/mingw_64/include/QtCore -isystem C:/Qt/6.6.3/mingw_64/include -isystem C:/Qt/6.6.3/mingw_64/mkspecs/win32-g++ -isystem C:/Qt/6.6.3/mingw_64/include/QtWidgets -isystem C:/Qt/6.6.3/mingw_64/include/QtGui -DQT_QML_DEBUG -g -std=gnu++17 -fdiagnostics-color=always -MD -MT CMakeFiles/CMakeLearn.dir/src/main.cpp.obj -MF CMakeFiles\CMakeLearn.dir\src\main.cpp.obj.d -o CMakeFiles/CMakeLearn.dir/src/main.cpp.obj -c C:/Users/User1/Documents/Qt_Projects/CMakeLearn/src/main.cpp C:/Users/User1/Documents/Qt_Projects/CMakeLearn/src/main.cpp:1:10: fatal error: mainwindow.hpp: No such file or directory 1 | #include "mainwindow.hpp" | ^~~~~~~~~~~~~~~~ compilation terminated. [3/5 0.5/sec] Building CXX object CMakeFiles/CMakeLearn.dir/src/mainwindow.cpp.obj FAILED: CMakeFiles/CMakeLearn.dir/src/mainwindow.cpp.obj C:\Qt\Tools\mingw1120_64\bin\g++.exe -DMINGW_HAS_SECURE_API=1 -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NEEDS_QMAIN -DQT_WIDGETS_LIB -DUNICODE -DWIN32 -DWIN64 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -D_UNICODE -D_WIN64 -IC:/Users/User1/Documents/Qt_Projects/build-CMakeLearn-Desktop_Qt_6_6_3_MinGW_64_bit-Debug/CMakeLearn_autogen/include -isystem C:/Qt/6.6.3/mingw_64/include/QtCore -isystem C:/Qt/6.6.3/mingw_64/include -isystem C:/Qt/6.6.3/mingw_64/mkspecs/win32-g++ -isystem C:/Qt/6.6.3/mingw_64/include/QtWidgets -isystem C:/Qt/6.6.3/mingw_64/include/QtGui -DQT_QML_DEBUG -g -std=gnu++17 -fdiagnostics-color=always -MD -MT CMakeFiles/CMakeLearn.dir/src/mainwindow.cpp.obj -MF CMakeFiles\CMakeLearn.dir\src\mainwindow.cpp.obj.d -o CMakeFiles/CMakeLearn.dir/src/mainwindow.cpp.obj -c C:/Users/User1/Documents/Qt_Projects/CMakeLearn/src/mainwindow.cpp C:/Users/User1/Documents/Qt_Projects/CMakeLearn/src/mainwindow.cpp:1:10: fatal error: mainwindow.hpp: No such file or directory 1 | #include "mainwindow.hpp" | ^~~~~~~~~~~~~~~~ compilation terminated. [4/5 0.5/sec] Building CXX object CMakeFiles/CMakeLearn.dir/CMakeLearn_autogen/mocs_compilation.cpp.obj ninja: build stopped: subcommand failed. 09:29:26: The process "C:\Program Files\CMake\bin\cmake.exe" exited with code 1. Error while building/deploying project CMakeLearn (kit: Desktop Qt 6.6.3 MinGW 64-bit) When executing step "Build" 09:29:26: Elapsed time: 00:09.
-
@KK2022 said in CMake File not found Project Sources:
#include "mainwindow.hpp"
Change to
#include "includes/mainwindow.hpp"
Or use https://cmake.org/cmake/help/latest/command/target_include_directories.html to specify the "includes" folder as a directory to look for header files.
-
@KK2022 said in CMake File not found Project Sources:
#include "mainwindow.hpp"
Change to
#include "includes/mainwindow.hpp"
C:/Users/User1/Documents/Qt_Projects/CMakeLearn/src/main.cpp:1:10: fatal error: includes/mainwindow.hpp: No such file or directory 1 | #include "includes/mainwindow.hpp" | ^~~~~~~~~~~~~~~~~~~~~~~~~ compilation terminated. [3/5 0.5/sec] Building CXX object CMakeFiles/CMakeLearn.dir/src/mainwindow.cpp.obj
It is still not found.
For target_include_directories, could you give me an example please.
target_include_directories(CMakeLearn PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/includes)
gives the error
[cmake] CMake Error at CMakeLists.txt:32 (target_include_directories): [cmake] Cannot specify include directories for target "CMakeLearn" which is not [cmake] built by this project.
-
@KK2022 said in CMake File not found Project Sources:
target_include_directories(CMakeLearn PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/includes)
Where did you put this line?
It needs to be after the definition of CMakeLearn target. -
@jsulm Thank you so much. I was under the impression that the Project Name CMakeLearn is already defined at the beginning of the file. So I put the line target include directories under setting the variable PROJECT_SOURCES.
Now I understand it has to be under add executable where the target is created. Now it is built and goes through.I have a question regarding the header files. Should the header files still be given under PROJECT_SOURCES?
set(PROJECT_SOURCES src/main.cpp includes/mainwindow.hpp src/mainwindow.cpp src/mainwindow.ui )
Or is it enough if we just add the .cpp files and .ui files? Will the
target_include_directories
add the header files from the directory provided? -