Reputation: 62553
I need to add a compile-only dependency to external lib my in CMake file - referred to as the_lib
later in the question. By saying compile-only dependency I mean propagating compile-time properties, most importantly -I
rules.
The library in question is created as a library target using add_library
. Normally I would simply use target_link_libraries(my_exec the_lib)
, but this adds both compile-time and link-time properties, i.e., this adds both -I
and -l/-L
rules to compilation commands, while I only need -I
. (If anyone is curios why I need such a setup, this is because reasons.)
Please note, target_include_directories
with something like ${the_lib_SOURCE_DIR}
(or anything similar to that effect) would not work for me, because it wouldn't add include directories necessary for the lib. I need something like ${the_lib_INCLUDE_DIRS}
where the_lib_INCLUDE_DIRS
would be populated to as -I rules required by the_lib
- but I didn't find any variable which would match that.
It is worth noting that I can't (or shan't) modify the the_lib
.
Upvotes: 2
Views: 1034
Reputation: 180103
I need to add a compile-only dependency to external lib my in CMake file
By an "external" lib, I take you to mean one that is not part of the same project -- i.e. one that is not configured within the scope of the same CMake build system as the target you're trying to build.
[...] Normally dependencies are added with
target_link_libraries(my_exec the_lib)
, but this adds both compile-time and link-time dependency.
Well no, not necessarily. As it says on the tin, that adds a link dependency, which you can think of as an -l
option. To the best of my knowledge, it does not generate any -I
options for external libraries, or otherwise have any impact on the compilation phase with regard to external libraries. Similarly, as far as I am aware, it propagates transitive dependencies of any sort only when the the added library is another target configured and built by the same build system. That is, only for internal libraries, not external ones.
Please note,
target_include_directories
with something like${the_lib_SOURCE_DIR}
(or anything similar to that effect) would not work for me, because it wouldn't add include directories necessary for the lib. I need something like${the_lib_INCLUDE_DIRS}
wherethe_lib_INCLUDE_DIRS
would be populated to as-I
rules required bythe_lib
- but I didn't find any variable which would match that.
I think you're asking for the include directories that would be necessary to successfully use the_lib
's headers, in a situation where those headers have their own external dependencies. There's a reason why you don't find a variable appropriate to that: there is no consistent or standard way to obtain that information for external libraries. The available techniques depend on the_lib
. They will include some, but probably not all, of the following:
the_lib
or with CMake itself to define a CMake variable conveying the wanted information.pkg-config
to read the information from a pkg-config entry associated with the_lib
.the_lib
, along the line of Python's python-config
.the_lib
's headers to determine the external packages they depend upon, and search explicitly, individually for those packages' headers.There is no magic variable or function in CMake to automatically glean such information, because generally speaking, it is not available from the_lib
's headers themselves. Note, too, that most of those would depend on the_lib
's include dependencies being installed in specific places anticipated at the_lib
's build time. That's pretty hard to ensure.
Upvotes: 1