dimba
dimba

Reputation: 27581

Listing header files in Visual Studio C++ project generated by cmake

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

Upvotes: 66

Views: 38415

Answers (5)

James Slater
James Slater

Reputation: 1

In my CMake setup, I use file(GLOB ...) to collect all files in a directory. Then, I loop through each file, including it with include(${FILE}). This method dynamically integrates all files into the build process.

#Define a variable to store all files within the specified directory
file(GLOB MY_DIRECTORY_FILES "${PROJECT_SOURCE_DIR}/*")

#Iterate over each file in the directory
foreach(FILE ${MY_DIRECTORY_FILES})
    # Include each file found in the directory
    include(${FILE})
endforeach()

Upvotes: 0

tjwrona
tjwrona

Reputation: 9035

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.

Upvotes: 4

metasim
metasim

Reputation: 4960

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.

Upvotes: 30

npclaudiu
npclaudiu

Reputation: 2441

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)

Upvotes: 9

tim_hutton
tim_hutton

Reputation: 878

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})

Upvotes: 47

Related Questions