I have a project where there is 1 static library (A) and 1 executable (E)
Linux, Ubuntu 24.04, cmake 3.28.3
The static library (A) itself links some system provided static libraries like this
target_link_libraries(A PUBLIC
-Wl,-Bstatic
${YAML_STATIC_LIBRARIES} ${MINIZIP_STATIC_LIBRARIES} ${PCAP_STATIC_LIBRARIES} ${SLIRP_STATIC_LIBRARIES}
-Wl,-Bdynamic
)
these have been found via pkg_search_module
.
Later in E, I do
target_link_libraries(E PUBLIC
-Wl,-Bstatic
${NCURSES_STATIC_LIBRARIES} ${LIBEVDEV_STATIC_LIBRARIES}
-Wl,-Bdynamic
A
)
so you can see that all system libraries are always inside the static
/ dynamic
linker commands.
In the final linker command for E, cmake rearranges the libraries needed by A breaking this invariant.
This is a snippet of the final link command
-o E -Wl,-Bstatic -lncursesw -ltinfo -ldl -levdev
-Wl,-Bdynamic libA.a -Wl,-Bstatic -Wl,-Bdynamic
-lyaml -lminizip -lz -lslirp -lglib-2.0 -lm -lpcre2-8
As you see it has moved A’s needed libraries outside the static
/ dynamic
pair.
The obvious solution is to move all libraries to E, but this causes information leakage and duplication (I still need the header files in A).
It looks like cmake is trying to interpret A’s dependencies and decided to rearrange the tokens.
Thanks
Andrea