Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake and VisualStudio: Group files in solution explorer

To finish a long coding session on a project, I wanted to test if my CPP project is compilable on an arrangement of OS'es.

I've been working in Win10 all the time. Compiles fine.
I've tried a Raspberry Pi. Compiles fine. I re-download a seperate copy of my project to a Win10 client, run cmake-gui, and open the project: My folder structure in the solution explorer all gone.

So I started digging around, and apparently this structure is kept in CMakeLists.txt with the command source_group. So I start adding more source_groupings to my cmake lists, and for some reason my groupings won't take.

Example:
source_group("game\\entitysystem" FILES ${entitysystem_SRC}) // Existing grouping source_group("game\\entitysystem\\components" FILES ${components_SRC}) // My new grouping

My glob would be this:

file(GLOB components_SRC "game/components/*.h" "game/components/*.cpp" )

file(GLOB entitysystem_SRC "game/entitysystem/*.h" "game/entitysystem/*.cpp" )

I do believe my GLOB's are correct since the new project-clone compiles fine. It's just that every part of the new structure in Visual Studio's Solution Explorer seems lost. Yes, I have cleared Cmake's cache and regenerated the project. Doesn't change it.

Original structure: Original folder structure

Cloned project structure: Cloned folder structure

Edit:

I did make a mistake in my source_group as in that it should not put components beneath entitysystem, but still, why aren't there any filters created in Visual Studio?

like image 482
MrKickkiller Avatar asked Oct 18 '22 21:10

MrKickkiller


1 Answers

First, make sure you are setting set_property(GLOBAL PROPERTY USE_FOLDERS ON).

Second, it is not recommended to use GLOB to collect a list of source files. From the file(GLOB documentation:

We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.

The recommended way to list project files is to add them by hand to CMakeLists.txt.

If you still want to GLOB, it looks like you want to mirror the directory structure in your source tree. You can use a macro such as this every place you define a library or executable to automatically sort them for you:

foreach(FILE ${SRCS}) 
    # Get the directory of the source file
    get_filename_component(PARENT_DIR "${FILE}" DIRECTORY)

    # Remove common directory prefix to make the group
    string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" GROUP "${PARENT_DIR}")

    # Make sure we are using windows slashes
    string(REPLACE "/" "\\" GROUP "${GROUP}")

    # Group into "Source Files" and "Header Files"
    if ("${FILE}" MATCHES ".*\\.cpp")
       set(GROUP "Source Files${GROUP}")
    elseif("${FILE}" MATCHES ".*\\.h")
       set(GROUP "Header Files${GROUP}")
    endif()

    source_group("${GROUP}" FILES "${FILE}")
endforeach()
like image 181
Cinder Biscuits Avatar answered Oct 21 '22 03:10

Cinder Biscuits