1

I am in the process of porting a big library project from Linux to Windows. Fortunately we were using CMake even before porting was even remotely on the table so not many adjustments were needed.

I figured it might be a good idea to develop the Windows parts natively on Windows for easier testing so i created a VS Studio using the CMake-gui

My project is organized like this:

lib/ # Library source code include/ mylib/ # Public installable header files

In the top CMakeLists.txt i added

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

so my internal source files include the headers just like an external application would.

The problem is now the following: Visual Studio 2015 displays the public header files in the "External References"-directory among a lot of system headers. It is not obvious which header files belong to the project.

How can i make VS display the public headers separately from the system includes?

3
  • 1
    All header files you want to be explicitly listed in Visual Studio projects have to be listed as a source file in your CMake target's list of sources. Then you can group those sources/headers via the source_group(... REGULAR_EXPRESSION ...) command. For more details see Keeping file hierarchy across subdirectories in CMake and How to keep source folders hierarchy on solution explorer?.
    – Florian
    Sep 15, 2015 at 12:16
  • If i were able to accept this comment as a correct answer, i'd do it. Thank you very much.
    – Richard
    Sep 17, 2015 at 9:17
  • You're welcome. Turned my comment into an answer. And I added an example implementation.
    – Florian
    Sep 17, 2015 at 16:03

1 Answer 1

2

Turning my comment into an answer

All header files you want to be explicitly listed by CMake in Visual Studio projects have to be listed as a source file in your CMake target's list of sources.

Then you can group those sources/headers via the source_group(... FILES/REGULAR_EXPRESSION ...) command.

If you have more then one target and you don't want to add those steps manually every time, you could think about grouping them into a function():

function(my_add_library _target)
    file(
        GLOB_RECURSE _header_list 
        RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
        "${CMAKE_SOURCE_DIR}/include/*.h*"
    )
    add_library(${_target} ${ARGN} ${_header_list})
    target_include_directories(${_target} PRIVATE "${CMAKE_SOURCE_DIR}/include")
    source_group("Public Headers" FILES ${_header_list})
endfunction(my_add_library)

Note:

For more details on grouping source/header files in CMake see:

And as a general reference:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.