c++ - Conditional transitive link libraries -
i'm struggling cmake "config"-based package description when using 2 depending packages upstream package has optional link libraries.
suppose have liba
optionally uses zip functionality (say option use_zlib
in liba
's cmakelists.txt
). means liba
optionally have -lz
in interface_link_libraries
(exported liba-config.cmake
). now, if have libb
using liba
via find_package(liba)
, how know if liba
has been built or without zlib support when both libraries installed , exported cmake's package config system?
i know link library information treated transitively , propagated link libraries of libb
, hence -lz
appear whenever linking against libb
. however, path
libz included; consequently, link fails "could not find library z" unless magically knew libz
link library of liba
, include from. including absolute path libz
in liba
's config not way go, ruins portability on different systems.
edit: i've been getting replies suggesting use target_link_libraries(liba private z)
, e.g. declaring (in fact internal liba) libz private library. unfortunately not solve issue, cmake automatically adds private
-marked link library interface_link_libraries
list via $<link_only:z>
. shows necessity of having -lz
appear in exported link library list in way (and cmake export script authors understand transitive issue).
the canonical way making liba-config.cmake
template , configure_file() populate information build options.
for example, cmakelists.txt
looks this:
if(zlib_found) ... # link zlib set(built_with_zlib 1) endif() ... # more options configure_file(alib-config.cmake.in alib-config.cmake)
now, liba-config.cmake.in
should have
set(built_with_zlib @built_with_zlib@) if(built_wth_zlib) # append lz alib_libraries or whetever variable called endif()
after configuration alib-config.cmake
contain set(built_with_zlib 0)
or set(built_with_zlib 1)
depending on value in cmake.
Comments
Post a Comment