Retrieving imported preprocessor definition


I’ve got a library L that may define some preprocessor definition MYDEF with target_compile_definitions(L PUBLIC MYDEF).

How do I check, in the cmake file of a client application A, if MYDEF is defined ?

L is properly installed and imported through find_package(L)


My issue may be a bit more complicated than anticipated and related to another issue: Findmodule transitivity?
What I want exactly do is:
L may depend on a third party library TP that is not properly packaged.
If L uses TP, it imports TP with a FindTP.cmake file and defines a compile definition TPNEEDED

What I’d like to do is, when, in A, I find_package(L) then TP is automatically linked with A, but only if L actually needed TP.

the FindTP.cmake does not seem to be used “transitively”, and I can’t retrieve if TPNEEDED is defined so far. Besides, I suppose that it may be only defined after find_package(L) that will generate an error because TP is not found, before I can do anything about it.

Thus, what would be the proper way to manage this optional dependency?


maybe you will read this: It’s Time To Do CMake Right | Pablo Arias

1 Like


Also an interesting link that rises some questions:
1- target_include_directories(JSONUtils … PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
Why is not the PRIVATE part within the BUILD_INTERFACE)
2- in the FindModule.cmake example, what does the pkg_check_modules function (I’ve just looked at but have nor really understood its usage)
3- what is the difference between RapidJSON_INCLUDE_DIR and RapidJSON_INCLUDE_DIRS and why are they not set in the same way?

I’m now checking if my practice is on line with the rest of the post but it seems to.

Anyway, it does not enlight me on my optional dependency issue…

Thanks by advance for more insights,


extract the properties you are interesing in form your imported target
but you are on the wrong way if you do it.

You should fix the required packages or build them yourself with fetch contents or CPM.cmake.

Hi, I didn’t know about CPM. I’ll look into it as it seems interesting.