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 ...
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 ...
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.
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:
\
or /
and the second character is not 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: