CMake folder path
-
I am currently trying to experiment with CMake by attempting to put my files in separate directories.
I am trying to put all my header files into the Includes folder and my implementation files into Sources folder. My project does indeed build and run. However, something strange is happening in the project hierarchy. It displays the Sources folder correctly, but it doesn't seem to be able to detect the Includes folder.
(I have created and added these folders using file explorer and then added them to my CMakeList)
CMakeList:
cmake_minimum_required(VERSION 3.5) project(QTextEditor VERSION 0.1 LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) 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) set(PROJECT_SOURCES # Header files # ----------------------------------------------------- # ----------------------------------------------------- # QMainWindows Includes/QMainWindows/MainWindow/mainwindow.h # QDialogs Includes/QDialogs/About/aboutdialog.h Includes/QDialogs/Find/finddialog.h Includes/QDialogs/Replace/replacedialog.h # Subclasses - QTextEdit Includes/Subclasses/QTextEdit/qtexteditsubclass.h # ----------------------------------------------------- # ----------------------------------------------------- # Implementation files # ----------------------------------------------------- # ----------------------------------------------------- # main application main.cpp # QMainWindows Sources/QMainWindows/MainWindow/mainwindow.cpp Sources/QMainWindows/MainWindow/mainwindow-connect-signal-slot.cpp Sources/QMainWindows/MainWindow/mainwindow-slots.cpp Sources/QMainWindows/MainWindow/mainwindow-methods.cpp # QDialogs Sources/QDialogs/About/aboutdialog.cpp Sources/QDialogs/Find/finddialog.cpp Sources/QDialogs/Replace/replacedialog.cpp # Subclasses - QTextEdit Sources/Subclasses/QTextEdit/qtexteditsubclass.cpp # ----------------------------------------------------- # ----------------------------------------------------- # UI files # ----------------------------------------------------- # ----------------------------------------------------- # QMainWindow mainwindow.ui # QDialogs aboutdialog.ui finddialog.ui replacedialog.ui # ----------------------------------------------------- # ----------------------------------------------------- # Resource files # ----------------------------------------------------- # ----------------------------------------------------- resources.qrc # ----------------------------------------------------- # ----------------------------------------------------- )
Image:
As you can see in the image above, the Sources folder is displayed correctly. But the Includes folder is no where to be found.
Why is this happening? Is this a bug in QtCreator?
-
Which version of Qt Creator are you using? If you open a header file in the text editor, where does it show in the Project Tree?
-
I don't have an explicit folder for source files or header files, but my Projects view behaves the way you want yours to. I do have a qt_add_executable() statement in which I enumerate all my sources and includes. I don't see this in your CMakeLists.txt file.
-
@Saviz try the following:
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/Includes )
and more if needed
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/Includes/QDialogs )if you like to split them, you can add them into two separate file definitions and then add them to your project. I did it this way.
file( GLOB MYPROJECT_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ Sources/QDialogs/About/aboutdialog.cpp ... ) file( GLOB MYPROJECT_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/ Sources/QDialogs/About/aboutdialog.h ... ) add_executable( ${PROJECT_NAME} ${MYPROJECT_SOURCES} ${MYPROJECT_HEADERS} )
this will be cleaner. Same with ui and qrc files.
-
@JoeCFD said in CMake folder path:
if you like to split them, you can add them into two separate file definitions and then add them to your project. I did it this way.
file( GLOB MYPROJECT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/ Sources/QDialogs/About/aboutdialog.cpp
...
)
file( GLOB MYPROJECT_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/ Sources/QDialogs/About/aboutdialog.h
...
)add_executable( ${PROJECT_NAME}
${MYPROJECT_SOURCES}
${MYPROJECT_HEADERS} )this will be cleaner.
Cleaner in which way? What does this help in the display issue above?
Since the op did not answer the questions from @cristian-adam no more help can be done here.
-
@Christian-Ehrlicher Maybe a cleaner way to organize his cmake file. I have include in my cmake file and he does not have it.
I am using 6.0.2 and Header Files and Sources Files are well split. -
- FILE(GLOB) is useless here and using globbing to collect the sources is discouraged
- It's a matter of taste if
file( GLOB MYPROJECT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/ Sources/QDialogs/About/aboutdialog.cpp
...
file( GLOB MYPROJECT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/ Sources/QDialogs/About/aboutdialog.cppor
set(MYPROJECT_SOURCES
Sources/QDialogs/About/aboutdialog.cpp
Sources/QDialogs/About/aboutdialog.h
Sources/QDialogs/About/aboutdialog.ui
...
)looks better - from my pov the latter one is more compact and therefore more readable.
-
@Christian-Ehrlicher True it is a taste thing. We did this long long ago and have got used to it.
I rechecked the usage of file. If all file names are explicitly added, GLOB is not needed.
It is also true that GLOB is not recommended. However, since we have a lot of files, it was simply applied. One problem is that some files are not used and will be included automatically in the project