Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake remove include directory

Tags:

c++

cmake

Is there any way to remove once found include directory in CMake. I found similar question here. But answer does not make much sense for me.

I need almost exactly same as author of the above mentioned message. I have a global CMakeLists.txt file which finds all neccessary include paths. However in CMakeLists.txt file of a one particular submodule, I want to "hide" or "remove" system include path and provide alternative include path.

Yes, one solution could be just simply change includes in the mentioned submodule, but this submodule is an external library from other repository, so I do not want to change its code.

like image 547
Michal Avatar asked Jun 04 '14 07:06

Michal


1 Answers

If you can afford to use a newer CMake version (2.8.11 or higher), prefer target_include_directories over include_directories and most of your problems should disappear right away.

The issue is that the old include_directories works on directory properties, which works out only if the physical layout of the files on the hard disk exactly matches the logical organisation of code in the different sub-projects. With more complex codebases, this is often difficult to achieve and leads to problems like the one you described. The new target_include_directories instead works on target properties, meaning that the list of include directories for a file is instead determined by which sub-project it belongs to. This is usually the more natural way to think about such build options.

If you are stuck with an older CMake version or you have to use include_directories for other reasons (which might be the case here, if the problem is an external library that you can not change), you can try tinkering with the INCLUDE_DIRECTORIES directory property that is being set by the include_directories command, but be prepared for some fiddling:

get_property(the_include_dirs DIRECTORY foo PROPERTY INCLUDE_DIRECTORIES)
string(REPLACE ${what_needs_to_go} "" new_include_dirs ${the_include_dirs})
set_property(DIRECTORY foo PROPERTY INCLUDE_DIRECTORIES ${new_include_dirs})
like image 78
ComicSansMS Avatar answered Sep 26 '22 05:09

ComicSansMS