# source\_group Visual Studio

**URL:** https://discourse.cmake.org/t/source-group-visual-studio/9990
**Category:** Code
**Tags:** os:windows, comp:msvc, gen:ninja
**Created:** [February 2, 2024, 10:45am UTC](https://discourse.cmake.org/t/source-group-visual-studio/9990 "2024-02-02T10:45:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jmigual](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jmigual/32/2936_2.png) [@jmigual](https://discourse.cmake.org/u/jmigual)
#### Post date: [February 2, 2024, 10:45am UTC](https://discourse.cmake.org/t/source-group-visual-studio/9990/1 "2024-02-02T10:45:52Z")

</div>

I am trying to use `source_group` to show the files nicely in my IDE (Visual Studio 2022). The structure of my header files has some files in subfolders and others only in the `include` folder (sorry I can only embed one photo):

```plaintext
include
├── DD
│ ├── comparator.hpp
│ ├── dd_solution.hpp
│ └── vertex.hpp
├── FORPFSSPSD
│ ├── FORPFSSPSD.h
│ ├── aliases.hpp
│ ├── plexity.hpp
│ └── ...
├── cyclefinder.h
├── delay.h
└── ...

```

And I use this CMake code to group the header files (`LIB_COMMON_HEADERS` has the list of all headers):

```cmake
source_group(
    TREE "${PROJECT_SOURCE_DIR}/include"
    PREFIX "Header Files"
    FILES ${LIB_COMMON_HEADERS}
)

```

However, in Visual Studio 2022 it shows as follows:

 ![image](https://discourse.cmake.org/uploads/default/original/2X/6/6d6f84af669df731d91cfc6000b58e029499f0ea.png)

Notice how the files immediately in the folder `include/*.h` and not in a subfolder are not added in to the “Header files” group.

I’ve also noticed that if I define the subgrup as follows (removing `/include` from the `TREE`):

```cmake
source_group(
    TREE "${PROJECT_SOURCE_DIR}"
    PREFIX "Header Files"
    FILES ${LIB_COMMON_HEADERS}
)

```

Then the files are grouped but all inside the `include` directory but not immediately inside the `Header Files` group:

```plaintext
Header Files
└── include
     ├── DD
     │ ├── comparator.hpp
     │ ├── dd_solution.hpp
     │ └── vertex.hpp
     ├── FORPFSSPSD
     │ ├── FORPFSSPSD.h
     │ ├── aliases.hpp
     │ ├── plexity.hpp
     │ └── ...
     ├── cyclefinder.h
     ├── delay.h
     └── ...

```

Am I doing something wrong? Shouldn’t the first code already work properly? If my code is fine, is this a bug in CMake or Visual Studio 2022?

---

<div class="post-metadata">

### Author: ![dnglaze](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/74df32/32.png) [@dnglaze](https://discourse.cmake.org/u/dnglaze)
#### Post date: [February 2, 2024, 5:21pm UTC](https://discourse.cmake.org/t/source-group-visual-studio/9990/2 "2024-02-02T17:21:28Z")

</div>

Are you opening this through the CMakeLists.txt and not the Visual Studio solution (if you are generating a Visual Studio solution)? It could very well be an issue with how Visual Studio builds the view from the CMake file API information. I suppose that could suggest how CMake reports the information could also be at fault. If you are generating a Visual Studio solution and not something like Ninja, you could take a look at the VS solution and see if the layout is correct.

At any rate, the way you are using `source_group` appears to be correct.

---

<div class="post-metadata">

### Author: ![jmigual](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jmigual/32/2936_2.png) [@jmigual](https://discourse.cmake.org/u/jmigual)
#### Post date: [February 3, 2024, 8:05am UTC](https://discourse.cmake.org/t/source-group-visual-studio/9990/3 "2024-02-03T08:05:25Z")

</div>

I’m opening this through the CMakeLists.txt option of Visual Studio so I guess then that it’s a Visual Studio issue. I’ll try to open an issue.

Thanks for the help 😊
