Visibility of some files in Visual Studio

Hello,

I am setting a library project for Visual Studio (questions at the end of post) :

└── MyLib
    ├── CMakeLists.txt
    ├── include
    │   └── MyLib
    │       └── SubLib_1
    │           └── x.h
    ├── src
    │   ├── CMakeLists.txt
    │   ├── SubLib_1
    │   │   └── x.cpp
    │   ├── SubLib_2
    │   │   ├── y.h
    │   │   └── y.cpp
    │   └── SubLib_3
    │       ├── y.h
    │       └── y.cpp
    └── test
        ├── CMakeLists.txt
        └── testcase.cpp

MyLib\CMakeLists.txt :

cmake_minimum_required(VERSION 3.17.3)

project(MyLib LANGUAGES CXX)

add_subdirectory(src)
add_subdirectory(test)

MyLib\src\CMakeLists.txt :


target_include_directories(MyLib
    PUBLIC
        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:include>
    PRIVATE
        ${CMAKE_CURRENT_LIST_DIR} )

target_sources(MyLib
    PRIVATE
        $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/MyLib/SubLib_1/x.h>
        SubLib_1/x.cpp
        SubLib_2/y.h
        SubLib_2/y.cpp
        SubLib_3/z.h
        SubLib_3/z.cpp)

source_group(TREE . PREFIX Sources)

When I run the generator Visual Studio 2017, I get a solution called MyLib, where I can found 4 projects :

  • ALL_BUILD
  • MyLib
  • MyLibTest
  • ZERO_CHECK

In the project MyLib, I have :

  • References
  • External Dependencies
  • Header Files
  • Source Files

In Header Files, I have :
y.h
z.h

In Source Files, I have :
x.cpp
y.cpp
z.cpp


  1. Why do the files are grouped (Header Files / Source Files) ? Yet I used the command source_group with the TREE option.
  2. How to keep in VS the sub-libraries directories structure (should I make a CMakeLists in each sublib ?) ?
  3. How to display the private headers ?

I finally got that, which seemts to work, regarding the presentation in the VS tree (but the source files are for now empty).

source_group(TREE . PREFIX Sources) didn’t work without giving the list of files. I don’t understand why.

I you think it could be improved in anyway, let me know. Thanks.

add_library(MyLib STATIC)

set(PUBLIC_SOURCE_LIST
    ${PROJECT_SOURCE_DIR}/include/MyLib/SubLib_1/x.h)

set(PRIVATE_SOURCE_LIST
    SubLib_1/x.cpp
    SubLib_2/y.h
    SubLib_2/y.cpp
    SubLib_3/z.h
    SubLib_3/z.cpp)

target_sources(MyLib
    PUBLIC
        ${PUBLIC_SOURCE_LIST}
    PRIVATE
        ${PRIVATE_SOURCE_LIST})

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX Sources FILES ${PRIVATE_SOURCE_LIST})
source_group(TREE ${PROJECT_SOURCE_DIR}/include/MyLib PREFIX Interface FILES ${PUBLIC_SOURCE_LIST})
        
target_include_directories(MyLib
    PUBLIC
        ${PROJECT_SOURCE_DIR}/include
    PRIVATE
        ${CMAKE_CURRENT_LIST_DIR} )

I suspect that test coverage of source_group is low. Could you please file an issue to investigate and either update the docs (or maybe figure out where it goes wrong)?

I created the issue.