What is the difference between "$ {CMAKE_CURRENT_SOURCE_DIR}" and ".". in INCLUDE_DIRECTORIES?

Should i use

INCLUDE_DIRECTORIES( . ) 

or

 INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ) 

What is the difference, if any? I saw mostly "." in existing code, but finding a point on the Internet is quite difficult ...

+5
source share
2 answers

Both options generate almost the same result, since CMake keeps track of the current directory and replaces the appearance of ".". from "$ {CMAKE_CURRENT_SOURCE_DIR} /.". The only difference is that "." option has an additional path component "/." attached.

Whatever you choose is a matter of taste.

+5
source

In your case, you might think of globally setting CMAKE_INCLUDE_CURRENT_DIR to ON .

Regarding your question, the answer depends mainly on your own preferences. I prefer the option of relative paths for the readability of your CMakeLists.txt files.

If you look at the CMake source code at cmTargetIncludeDirectoriesCommand :: Join () and SystemTools :: FileIsFullPath (), you will find the following conditions noted by CMake - after expanding the variables - if it adds CMAKE_CURRENT_SOURCE_DIR to the include paths:

  • As a rule, it should not contain generator expressions
  • On Windows, everything that doesn't start with \ or / , and the second character is not :
  • On Unix, anything that doesn't start with / or ~

As a result, the following CMake code

 include_directories(.) get_directory_property(_inc_dirs INCLUDE_DIRECTORIES) message("_inc_dirs: ${_inc_dirs}") 

will show

 _inc_dirs: [...your CMakeLists.txt path ...]/. 

This automatic and absolute behavior of the CMake path path prefix makes sense because it is possible - and often recommended - to build the tree outside the source code in CMake (see also CMake policy CMP0021 ).

You might consider setting CMAKE_USE_RELATIVE_PATHS to ON , which will convert include paths when generating the build environment in a path relative to your CMAKE_BINARY_DIR directory (but it only works with Makefile generators).

Some sitelinks include:

+2
source

All Articles