List of header files in a Visual Studio C ++ project created by cmake

I am creating a cmake-based build system for our product. The problem is that the Visual Studio project created by cmake does not display the header files in the solution browser.

What do I need to add to CMakeList.txt to view header files? The preferred solution is no need to list each specific header file.

Solution Here is the solution I came with:

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

thank

+50
c ++ visual-studio-2008 cmake
Jul 22 '09 at 18:13
source share
4 answers

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}) 
+40
Jul 23 '09 at 9:08
source share
— -

The main trick is to add header files to one of the goals (executable or library). This is especially annoying because cmake already knows all the dependencies of the file header 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 must do the same in Xcode too.

+22
Apr 14 '10 at 15:57
source share

I had the same problem while working in the build system for the Qt project, and I came out with this solution thanks to the other posts on this page. I have 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) 
+9
Apr 30 2018-11-18T00:
source share

I know this answer was very late in the game, but in later versions of Visual Studio you can change the "CMake Target Mode" view to "Folder View".

enter image description here

In this folder view, you can see all of your header files.

To be honest, I would just change the view in Visual Studio instead of modifying CMake files using special Windows hacks every day.

0
Apr 21 '19 at 2:28
source share



All Articles