Hi,
how should we model the following situation in CMake?
- There is an interface specification (e.g. set of C language header files):
H - There are two (static) libraries
L1andL2, each of them implementing this interfaceH - There is a (static) library
Uwhich uses theHinterface but does not care whetherL1orL2is used to implement that interface - There is another (static) library
Mwhich usesU - Finally, there are two executables
E1andE2which both useMand additionallyL1orL2, respectively.
The goal is that E1 pulls in M which in turn pulls in U and that the unresolved symbols (from the H interface) are resolved using L1. (Similarly for E2.)
Our current approach is to
- define an
INTERFACElibraryH - define “normal” libraries
L1andL2 - define a “normal” library
Uand specifytarget_link_libraries(U PRIVATE H) - define a “normal” library
Mand specifytarget_link_libraries(M PRIVATE U) - define executables
E1andE2and specifytarget_link_libraries(E1 PRIVATE M L1)andtarget_link_libraries(E2 PRIVATE M L2)
Unfortunately, the resulting sequence of libraries linked with the executables is
M- one of
L1andL2 U
This is no wonder as U does not depend on L1/L2. Nevertheless, U is linked too late resulting in unresolved symbols.
Is there some mechanism which supports the following specifications?
- “Whenever some item is used which depends on
H, a library which implementsHmust be linked afterwards” - "
L1implements theHinterface"
(Of course, for each executable, there may only be one library which implements H.)
An alternative could potentially be to have CMake link depth first. However, I currently lack imagination of possible draw backs of this approach.
Kind regards
Ingolf
Edit: Added suggestion for depth first link order.