I have following libraries lib_A
, lib_B
, lib_C
, lib_D
. I am doing something like this in my CMake files (order is important):
ADD_LIBRARY(lib_A)
ADD_LIBRARY(lib_B)
ADD_LIBRARY(lib_C)
ADD_LIBRARY(lib_D)
TARGET_LINK_LIBRARIES(lib_B lib_C)
TARGET_LINK_LIBRARIES(lib_A lib_B)
ADD_EXECUTABLE(Exec)
TARGET_LINK_LIBRARIES(exec lib_A)
TARGET_LINK_LIBRARIES(exec lib_D)
This results in following linker command.
linker -llib_A -llib_D -llib_B -llib_C
Q1. Why are lib_B
and lib_C
after lib_D
?
Q2. When I change CMake a little bit and do something like this:
TARGET_LINK_LIBRARIES(lib_A lib_D)
TARGET_LINK_LIBRARIES(exec lib_A)
then linking order is like this:
linker -llib_A -llib_B -llib_C -llib_D
Here, lib_B
and lib_C
are before lib_D
. It means that target_link_libraries
works differently for executable targets and library targets. Am I right?
The problem here is that lib_B
and lib_C
also depend on lib_D
, but I don't want to make target_link_libraries(lib_B lib_D)
and target_link_libraries(lib_C lib_D)
, because I have more of such cases, and I would have to do it manually for each library.
Of course doing like in Q2 solves the problem but:
Q3 - Is this order guaranteed somehow by CMake or it is just a fortuity?
Thank you