[CMake] Exporting/Publishing settings (e. g. include directories) from subdirectories

Jakob van Bethlehem jsvanbethlehem at gmail.com
Wed Jul 15 15:30:41 EDT 2015


Hej Rainer,

> 
> project-dir
> +- component1
> |  +- src
> |  +- include-dir1
> |  +- include-dir2
> +- component2
> 
> Let's say I want "component2" to add "include-dir1" and "include-dir2"
> from "component1" as include directories without "component2" knowing
> that these two directories exist.
> 

There is no way this is possible afaik, because if component2 uses any header file in one of the include directories, you would need to pass these directories to your compiler (most of the times using a -I or /I option depending on the OS).

However,
is it maybe possible that what you’re *really* asking is, whether it would be possible to say something like:
	target_link_libraries(component1 component2)
*without* explicitly spilling out the include directories, which would look something like:
	target_include_directories(component1 PRIVATE $<OBJECT_PROPERTY:component1,INTERFACE_INCLUDE_DIRECTORIES>)
 then the answer would be ‘yes’. You’d need to study the concept of IMPORTed and EXPORTed targets in CMake, see for instance http://www.cmake.org/cmake/help/v3.0/command/add_executable.html <http://www.cmake.org/cmake/help/v3.0/command/add_executable.html>, http://www.cmake.org/cmake/help/v3.0/command/add_library.html <http://www.cmake.org/cmake/help/v3.0/command/add_library.html> and http://www.cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets <http://www.cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets>

These concepts were originally created to nicely handle external dependencies (check out for instance the Qt5 documentation on integration with CMake, see http://doc.qt.io/qt-5/cmake-manual.html - you simply link with the proper Qt5 library, and these are set up such that automatically all required include folders are added to the list of include locations to build your component). However, with some work (essentially, creating some wrapper functions/macros) it can just as easily be made to work for dependencies inside a project. I don’t know about details, I just know that the infrastructure guys in the team I’m working in, made it work like that (borrowing a lot from the CMake files that come with Qt5) and it works like a charm!

> Therefore my question: Is there a way to import project
> settings/properties which have been exported from other
> subdirectories? And is cmake intended to be used like that?
> 
> I don't know whether I use the correct terms here but I hope that the
> idea is clear.

If you meant to ask what I wrote above, then the question was entirely correct, but the idea clear (in particular because I use it that way) and yes, there is a way. Whether CMake is ‘intended to be used like that I really wouldn’t know. My feeling is that this is definitely not the original intention of the feature. However, looking at how it works, this approach certainly also will not break as long as the feature is there.

Sincerely,
Jakob van Bethlehem
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150715/20d0160f/attachment.html>


More information about the CMake mailing list