4

I'm reading the cmakelist file of a big project, and confused about the usage of target_include_directories and include_directories.

The include_directories should be able to add all header files to path where the compiler search for. But I still see some target_include_directories in the cmakelist file of sub directories, that specify some include path for a specific target.

Can anybody explain the usage of these two? As far as I concerned, add all paths of header file to include_directories should be enough.

Ziqi Liu
  • 2,931
  • 5
  • 31
  • 64
  • The `include_directories` command could be used to add global directories that are needed by all (or at least multiple) targets. The `target_include_directories` command could be used to add single paths just needed by the specific target. – Some programmer dude Aug 22 '18 at 06:29

1 Answers1

10

include_directories applies to all the targets in a particular CMakeLists.txt file. For example, let's say you have

include_directories( ../include
   ${SOME_OTHER_PATH}/include
)

add_library(math ${MATH_SOURCES})

target_include_directories(math 
    math_include
)

add_executable(calculator ${MYCALCULATOR_SOURCES})   

target_include_directories(calculator 
    calc_include
)

calculator is an executable target and math is a library target defined in the same CMakeLists.txt. The folders ../include and ${SOME_OTHER_PATH}/include are visible to both. That means cmake will add the option -I../include -I<expanded-some-other-path>/include to both these targets when their sources are compiled.

For target_include_directories, the include path calc_include applies only to the calculator target and math_include applies only to the math target. As specified, math_include and calc_include would be (usually) subfolders present in the same folder that contains the CMakeLists.txt file.

Sriram
  • 345
  • 2
  • 14