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
L1
andL2
, each of them implementing this interfaceH
- There is a (static) library
U
which uses theH
interface but does not care whetherL1
orL2
is used to implement that interface - There is another (static) library
M
which usesU
- Finally, there are two executables
E1
andE2
which both useM
and additionallyL1
orL2
, 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
INTERFACE
libraryH
- define “normal” libraries
L1
andL2
- define a “normal” library
U
and specifytarget_link_libraries(U PRIVATE H)
- define a “normal” library
M
and specifytarget_link_libraries(M PRIVATE U)
- define executables
E1
andE2
and 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
L1
andL2
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 implementsH
must be linked afterwards” - "
L1
implements theH
interface"
(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.