0

My understanding is, that for larger projects the best practice is to use a CMakeLists.txt per folder.

I have the following structure (though this will increase in the future with more nested folders):

QPV
 |
 |---- CMakeLists.txt
 |---- 3rdparty
 |      |
 |      |---- glfw
 |---- src/
 |      |
 |      |---- CMakeLists.txt
 |      |---- main.cpp
 |      |---- VulkanRenderer.h
 |      |---- VulkanRenderer.cpp
 |      |---- utils/
 |      |       |
 |      |       |---- CMakeLists.txt
 |      |       |---- VulkanUtils.h
 |      |       |---- VulkanUtils.cpp

I want to mirror my subfolders in Visual Studio as they are on disk. My understanding is, source_group can achieve this, but I haven't been able to get it to work.

My three current CMakeLists files contain: QPV/CMakeLists.txt:

project(QPV VERSION 0.1)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
add_subdirectory(src)
# GLFW stuff
# Vulkan stuff

QPV/src/CMakeLists.txt:

add_executable(QPV
    main.cpp
    VulkanRenderer.h
    VulkanRenderer.cpp
)

include_directories(src PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

add_subdirectory(utils)

and finally QPV/src/utils/CMakeLists.txt

target_sources(${PROJECT_NAME} PUBLIC
    VulkanUtils.h
    VulkanUtils.cpp
)

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

set(ROOT_DIR "${CMAKE_SOURCE_DIR}/src")

source_group(TREE "${CMAKE_SOURCE_DIR}" 
    PREFIX "utils"
    FILES
    Vulkanutils.h
    VulkanUtils.cpp
)

I'm pretty sure my error is in the source_group command is what I did wrong, but I can't figure out how to fix it. Can I even do this from a nested folder? From what I could see online it looked like this command would go into the root folder's CMakeLists.txt but that would make the best practice of one CMakeLists.txt per folder obsolete.

Note that I don't want utils to be a different library, it still belongs to the QPV project. I just want my source files to not lie next to each other if sorting them into folders makes more sense.

Tare
  • 482
  • 1
  • 9
  • 25
  • I'm not sure, but this might be a duplicate of https://stackoverflow.com/q/31422680/11107541 or https://stackoverflow.com/q/33808087/11107541. check those out and see if anything there helps you. – starball Jun 29 '23 at 22:31
  • It looks like it solves the problem of the filter, but it uses a single CMakeLists file. I was hoping for a way to do it from within the nested CMakeLists file (or an explanation, that using the nested files is not really a best practice). Either way, thanks for the hint! – Tare Jun 30 '23 at 09:11

0 Answers0