Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing include_directories in CMake

Tags:

cmake

I have a cmake build in which I'm searching for a bunch of dependencies, i.e. I have many instances of:

FIND_PACKAGE(SomePackage) if(SOMEPACKAGE_FOUND)   include_directories(${SOMEPACKAGE_INCLUDE_DIR})   link_libraries(${SOMEPACKAGE_LIBRARIES}) endif(SOMEPACKAGE_FOUND) 

Now, I want to add a custom command to build a precompiled header file, but to do this I need to know all of the paths added by my include_directories calls. How can I get a list of these directories (preferably with the proper -I/path/to/directory format) so that I can add them to my custom command?

like image 728
rcv Avatar asked Aug 01 '11 17:08

rcv


People also ask

What does Include_directories do in CMake?

Add include directories to the build. Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory.

How add include path CMake?

First, you use include_directories() to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable() or add_library() call.

How do you clean after CMake?

Simply delete the CMakeFiles/ directory inside your build directory. rm -rf CMakeFiles/ cmake --build . This causes CMake to rerun, and build system files are regenerated. Your build will also start from scratch.

What are include directories?

The Include Directories corresponds to the environment variable INCLUDE . Directory settings displayed in the window are the directories that Visual Studio will search for include files referred to in your source code files. Corresponds to environment variable INCLUDE.


2 Answers

You can use the get_property command to retrieve the value of the directory property INCLUDE_DIRECTORIES

Something like this:

get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES) foreach(dir ${dirs})   message(STATUS "dir='${dir}'") endforeach() 

The value of this directory property only tracks the include_directories commands that have occurred previously in the same CMakeLists file, or that have been inherited from previous occurrences in a parent CMakeLists file. If your find_package and include_directories commands are scattered about throughout many subdirectories, this becomes a challenging issue.

If you get to that point, you may consider overriding the include_directories command with your own function or macro and track the values passed to it yourself. Or, simply accumulate them in a global property or an internal cache variable alongside each call to include_directories.

See the documentation here:

http://cmake.org/cmake/help/v2.8.8/cmake.html#command:get_property

http://cmake.org/cmake/help/v2.8.8/cmake.html#prop_dir:INCLUDE_DIRECTORIES

like image 81
DLRdave Avatar answered Oct 09 '22 22:10

DLRdave


Our solution to write include directories added by conan to a file

function(output_conan_include_dirs)     set(include_dir_str "")     foreach(DIR ${CONAN_INCLUDE_DIRS})         set(include_dir_str "${include_dir_str}${DIR}\n")     endforeach()      set(pth "${CMAKE_BINARY_DIR}/conan-includes.txt")     file(WRITE "${pth}" "${include_dir_str}")     message(STATUS "Conan include directories written to: ${pth}") endfunction()  output_conan_include_dirs() 
like image 42
radam Avatar answered Oct 09 '22 22:10

radam