INCLUDE_DIRECTORIES target property not reporting "indirect" ones


in my project, there is

add_library(foo source1.c)
target_include_directories(foo PUBLIC foo_library_includes)

add library(bar source2.c)
target_link_libraries(bar PRIVATE foo)

Whenever I actually compile bar, foo_library_includes is correctly added to the include path.

However, I’d like to use a function like the following

function(add_special_target library)
    get_target_property(dirs ${library} INCLUDE_DIRECTORIES)
    transmogrify(dirs) # macro prepends some prefix to each element in dirs
        COMMAND "special_processor ${dirs} further_arguments"

In fact, the custom command does a bit more, but I hope you get the intention.

Unfortunately, when using add_special_target(bar) after all the above definitions, the get_target_property() will not deliver the foo_library_includes although they are made applicable when building the bar target.

What is the correct way to solve the problem? (CMake 3.21.3 here)

I acknowledge that similar questions have been posted before (like this or this one). However I do not yet see how generator expressions would help me here: set (dirs $<TARGET_PROPERTY:${library},INCLUDE_DIRECTORIES>) instead of get_target_property() won’t work, right?

Kind regards

Use generator expressions. You can do something like this to put a prefix on each (in this case, -I, quotes, and newlines):


I stumbled upon exactly the same problem recently and was sadly unable to solve this till now.

I created a simple MWI [see - as I am unable to upload a file here], where the configure output is as follows:

lib1_include: TestingCMake/dir1/include
lib2_include: TestingCMake/dir2/include
lib1_interface_include: TestingCMake/dir1/include
lib2_interface_include: TestingCMake/dir2/include
lib1_link: lib1_link-NOTFOUND
lib2_link: lib1
– Configuring done
– Generating done

If I understand the CMake documentation for INTERFACE_INCLUDE_DIRECTORIES correctly [see here]:

When target dependencies are specified using `target_link_libraries(), CMake will read this property from all target dependencies to determine the build properties of the consumer.

, CMake does so somewhere at some point anyway. Is there a way to query those “build properties”? I.e. I want:

lib2_interface_include: TestingCMake/dir2/include;TestingCMake/dir1/include



Not at configure time. Only generator expressions can access that information (because it is only then when everything is known to answer such questions).

Hi Ben,

thanks a lot. Great support. This clears it up for me.