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

4k Views Asked by At

Should I use

INCLUDE_DIRECTORIES(
    .
)

or

INCLUDE_DIRECTORIES(
    ${CMAKE_CURRENT_SOURCE_DIR}
)

What is the difference, if any? I've seen mostly "." in existing code, but searching for a dot on the Internet is kind of difficult ...

2

There are 2 best solutions below

0
On

In your case you could think about globally setting CMAKE_INCLUDE_CURRENT_DIR to ON.

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

If you look into CMake's source code at cmTargetIncludeDirectoriesCommand ::Join() and SystemTools::FileIsFullPath() you find the following conditions checked by CMake - after expanding the variables - if it will append CMAKE_CURRENT_SOURCE_DIR to the include paths:

  • Generally it should not contain a generator expression
  • On Windows everything that not starts with a \ or / and the second character is not a :
  • On Unix everything that not starts with a / or ~

In consequence 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 path prefixing behaviour of CMake makes sense because it's possible - and often recommended - to do out-of-source tree builds in CMake (see also CMake policy CMP0021).

You can think about setting CMAKE_USE_RELATIVE_PATHS to ON which will convert the include paths back during generation of the build environment to paths relative to your CMAKE_BINARY_DIR directory (but it works only with the Makefile generators).

Some additional references:

0
On

Both uses generate nearly the same output, since CMake tracks the current directory and replaces the occurrence of "." with "${CMAKE_CURRENT_SOURCE_DIR}/.". The only difference is that the "." variant has the extra path component "/." appended.

Whichever you choose is a matter of taste here.