64

I'm building a cmake based build system for our product. The problem is that Visual Studio project, generated by cmake, doesn't display header files in solution browser.

What I need to add in CMakeList.txt to list header files? The preferred solution is where no need to list each particular header file.

Solution Here is a solution I came with:

file(GLOB_RECURSE INCS "*.h")
add_library(myLib ${SRCS} ${INCS})

Thanks

dimba
  • 26,717
  • 34
  • 141
  • 196

4 Answers4

46

Just add the header files along with the source files:

PROJECT (Test)

ADD_EXECUTABLE(Test test.cpp test.h)

Or using variables:

PROJECT (Test)

SET(SOURCE
  test.cpp
)

SET(HEADERS
  test.h
)

ADD_EXECUTABLE(Test ${SOURCE} ${HEADERS})
tim_hutton
  • 838
  • 9
  • 9
  • 9
    IMO the thing all of these answers miss (with the exception of @Simeon) is that you don't want to explicitly list all of the header files, because: 1. The headers listed as explicit dependencies can easily drift out of sync with those you are actually using in the source, and 2. CMake knows already which headers you use, so it's redundant to input them manually as well. So we really need an answer that takes CMakes own knowledge of the header dependencies, and sticks them into a group for the solution. – Jeff Trull Jan 25 '13 at 18:43
  • 1
    I appreciate that the dependency scanner feature may only work for some languages... but it does work for C++, and it would be great to have this knowledge CMake already possesses available to users of Visual Studio. If not that, at least have some sort of hook so we can write it ourselves in CMake code (via source_group). An explicit list of dependencies will definitely go stale over time, as the compile will keep working even if it's wrong. Plus, it's redundant... – Jeff Trull Jan 27 '13 at 22:43
  • Works for me after combining with source_group("My Headers" FILES ${MY_HDRS}) – Viet Jun 08 '13 at 23:38
27

The basic trick to this is to add the header files to one of the targets (either executable or library). This is particularly irritating because cmake already knows all the header file dependencies and should take care of this for us. You can organize it further using the source_group command:

  source_group("My Headers" FILES ${MY_HDRS})

Note that you have to do the same thing in Xcode too.

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
metasim
  • 4,793
  • 3
  • 46
  • 70
9

I had the same problem while working at a build system for a Qt project and I came out with this solution, thanks to the other posts on this page. I included a complete example adapted from my makefiles. Hope this helps!

cmake_minimum_required (VERSION 2.6) 
project (DemoSolution)

find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})

include_directories (../../include/)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

file(GLOB Demo_SOURCES *.cpp)
file(GLOB Demo_HEADERS *.hpp)
file(GLOB Demo_FORMS *.ui)
file(GLOB Demo_RESOURCES resources.qrc)

qt4_wrap_cpp(Demo_MOC ${Demo_HEADERS})
qt4_wrap_ui(Demo_FORMS_HEADERS ${Demo_FORMS})
qt4_add_resources(Demo_RESOURCES_RCC ${Demo_RESOURCES})

source_group("Headers" FILES ${Demo_HEADERS})
source_group("MOC" FILES ${Demo_MOC})

set(QT_USE_QTNETWORK, true)
set(QT_USE_QTSQL, true)
set(QT_USE_QTXML, true)

add_library(Demo SHARED
    ${Demo_SOURCES}
    ${Demo_HEADERS}
    ${Demo_MOC}
    ${Demo_FORMS_HEADERS}
    ${Demo_RESOURCES_RCC}
    )

target_link_libraries(Demo ${QT_LIBRARIES})
add_definitions(-D_DEMO_EXPORTS)
npclaudiu
  • 2,401
  • 1
  • 18
  • 19
  • 6
    It is not good form to do globbing. http://stackoverflow.com/questions/1027247/best-way-to-specify-sourcefiles-in-cmake. From the cmake 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. – Vitali Dec 15 '11 at 18:32
2

I know this answer is really late to the game, but in more recent versions of visual studio you can change the view from "CMake Target Mode" to a "Folder View"

enter image description here

In this folder view you will be able to see all of your header files.

To be honest i'd take simply changing the view in Visual Studio over modifying CMake files with Windows specific hacks any day.

tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
  • What version of VS has this because I'm using VS 2017 Pro and there is no `CMake Target View` thingy? :/ – Daniel Sep 25 '19 at 09:22
  • @Daniel, I have been using VS 2019, I would suggest updating because the CMake support has improved a lot in the latest version. – tjwrona1992 Oct 29 '19 at 02:15
  • Note that this doesn't work with out-of-source builds. – Alexander Revo Nov 16 '19 at 14:49
  • @AlexanderRevo that isn't necessarily true. We have a project at work that does an out of source build that lets you view CMake targets in Visual studio like this. The configuration for the project is ridiculously complex though so I'm not sure how they got it to work. – tjwrona1992 Nov 21 '19 at 20:29