9

Anyone knows how to put the source and header files on the solution root without vs filters at all? I have all my source subfolders nicely sorted into vs filters, however the files into the source root directory are going to "Header Files" and "Source Files".

Thanks.

pisiiki
  • 121
  • 3

2 Answers2

7

You can give an empty string to source_group to achieve this:

source_group("" FILES ${MY_TOP_LEVEL_SOURCES} ${MY_TOP_LEVEL_HEADERS})
ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • 1
    Didn't work for me. However, `" "` (a single space for filter group name) seems to work. – jimvonmoon May 26 '15 at 22:33
  • 1
    Seems like empty string name works only with FILES, not with REGULAR_EXPRESSION (CMake 3.5.2). A single space in a filter name makes my Visual C++ 2008 error while parsing vcproj XML. – Alex Che Apr 28 '16 at 08:17
0

The other answer was not enough for me as it flattens all the filesystem hierarchy. I don't know other simple solutions so I just recursed all the hierarchy and create filters accordingly. In this way the filter is the equivalent of a filesystem view:

# EXCLUDE_CURRENT to exclude source in the current directory
function(glob_source sources curdir)
    glob_source_recurse(sources ${curdir} "" ${ARGN})
    # Need to set sources in the parent scope https://stackoverflow.com/a/50953059/213871
    set(${sources} ${${sources}} ${SOURCE_FILES} PARENT_SCOPE)
endfunction()

macro(glob_source_recurse sources curdir prefix)
    subdirlist(subdirs ${curdir})
    foreach(subdir ${subdirs})
        if ("${prefix}" STREQUAL "")
            glob_source_recurse(sources "${curdir}/${subdir}" "${subdir}")
        else()
            glob_source_recurse(sources "${curdir}/${subdir}" "${prefix}/${subdir}")
        endif()
    endforeach()

    set (extra_macro_args ${ARGN})
    if (NOT extra_macro_args STREQUAL "EXCLUDE_CURRENT")
        file(GLOB SOURCE_FILES "${curdir}/*.cpp" "${curdir}/*.h" "${curdir}/*.hpp")
        source_group("${prefix}" FILES ${SOURCE_FILES})
        list(APPEND sources ${SOURCE_FILES})
    endif()
endmacro()

It can be used with:

glob_source(SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR})

You can edit according to your needs.

ceztko
  • 14,736
  • 5
  • 58
  • 73