Add Directories to Compiler Include Path

Other topics

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.

Syntax:

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

Parameters:

ParameterDescription
dirNone 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

Contributors

Topic Id: 5968

Example Ids: 20901

This site is not affiliated with any of the contributors.