Issue
I'm using cmake to generate a visual c++ project and I want to add a visual studio variable to my include path.
For normal variables with a hardcoded path I can simply do include_directories(PathToFolderIWantToInclude)
However now I want to do the same thing with a variable visual studio already knows about. $(KIT_SHARED_IncludePath)
I tried simply doing include_directories($(KIT_SHARED_IncludePath))
However that yields c:\path\to\my\project\$(KIT_SHARED_IncludePath). I also tried storing the value in a variable and using the variable instead however that didn't work either. How do I prevent cmake from prepending my current path onto the include path?
Solution
The problem is that CMake doesn't have a straight way to pass a relative path in the include_directories
and have it not preppended with the value of CMAKE_CURRENT_SOURCE_DIR
variable(setting which to nothing doesn't help either). But given the VS macros expand to the absolute paths we could try to circumvent cmake guards by providing a VS macro as follows:
include_directories("\\\\?\\$(KIT_SHARED_IncludePath)")
It will do the trick because it is an absolute path for sure and the correct path will be added as an additional include path to the compiler. Alas, in 2016 MS tool, which is MSVC compiler, doesn't know how to handle the very paths MS introduced many years ago. So this way of doing things is for the people who lives in a better time when MSVC knows how to handle the extended-length paths.
But we are not done yet. We have one more way to circumvent this contorversial CMake behavior: we can use generator expressions. The one we can use is at the top of the list:
include_directories($<1:$(KIT_SHARED_IncludePath)>)
That way CMake doesn't stand a chance and has to comply — the correct line gets added to the list of the additional includes. But it has a side effect: CMake starts complaining(gives a warning, not a error) about a relative path in the include_directories
command which you can shut up with either the following command:
cmake_policy(SET CMP0021 OLD)
Or run cmake with the following parameter each time: -Wno-dev
. But the latter one will disable all the warnings so I consider the first option preferable in the case.
UPD: Found even easier way to achieve the same. Just add the following line to your cmake file:
add_definitions(-I$(KIT_SHARED_IncludePath))
Answered By - ixSci Answer Checked By - Senaida (WPSolving Volunteer)