cmake

Add Directories to Compiler Include Path

Syntax#

  • include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 …])

Parameters#

Parameter Description
dirN one ore more relative or absolute paths
AFTER, BEFORE (optional) whether to add the given directories to the front or end of the current list of include paths; default behaviour is defined by CMAKE_INCLUDE_DIRECTORIES_BEFORE
SYSTEM (optional) tells the compiler to tread the given directories as system include dirs, which might trigger special handling by the compiler
## Add a Project’s Subdirectory
Given the following project structure
```
include\
myHeader.h
src\
main.cpp
CMakeLists.txt
```
the following line in the CMakeLists.txt file
```
include_directories(${PROJECT_SOURCE_DIR}/include)
```
adds the include directory to the include search path of the compiler for all targets defined in this directory (and all its subdirectories included via add_subdirectory()).

Thus, the file myHeader.h in the project’s include subdirectory can be included via #include "myHeader.h" in the main.cpp file.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow