Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 745
dimba Avatar asked Jul 22 '09 18:07

dimba


People also ask

Where can I find header files in Visual Studio?

Visual Studio looks for headers in this order: In the current source directory. In the Additional Include Directories in the project properties (Project -> [project name] Properties, under C/C++ | General). In the Visual Studio C++ Include directories under Tools → Options → Projects and Solutions → VC++ Directories.

How do I include a .h file in CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .

How do I add a header file in Visual Studio code C?

Place your caret on the first line of any C# or Visual Basic file. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select Add file header. To apply the file header to an entire project or solution, select Project or Solution under the Fix all occurrences in: option.


2 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}) 
like image 116
tim_hutton Avatar answered Sep 20 '22 20:09

tim_hutton


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.

like image 36
metasim Avatar answered Sep 16 '22 20:09

metasim