
Traditionally CMake generates link lines by starting with the direct link dependencies specified by `LINK_LIBRARIES` in their original order and then appending indirect dependencies that the direct dependencies do not express. This gives projects control over ordering among independent entries, which can be important when intermixing flags and libraries, or when multiple libraries provide the same symbol. However, it may also result in inefficient link lines. Add support for an alternative strategy that can reorder direct link dependencies to produce more efficient link lines. This is useful for projects that cannot easily specify their targets' direct dependencies in an order that satisfies indirect dependencies. Add a `CMAKE_LINK_LIBRARIES_STRATEGY` variable and corresponding `LINK_LIBRARIES_STRATEGY` target property to select a strategy. Fixes: #26271
16 lines
598 B
CMake
16 lines
598 B
CMake
enable_language(C)
|
|
|
|
add_library(A STATIC BasicA.c BasicX.c)
|
|
add_library(B STATIC BasicB.c BasicX.c)
|
|
add_library(C STATIC BasicC.c BasicX.c)
|
|
target_link_libraries(B PRIVATE A)
|
|
target_link_libraries(C PRIVATE A)
|
|
target_compile_definitions(A PRIVATE BASIC_ID="A")
|
|
target_compile_definitions(B PRIVATE BASIC_ID="B")
|
|
target_compile_definitions(C PRIVATE BASIC_ID="C")
|
|
|
|
add_executable(main Basic.c)
|
|
target_link_libraries(main PRIVATE A B C)
|
|
set_property(TARGET main PROPERTY LINK_DEPENDS_DEBUG_MODE 1) # undocumented
|
|
set_property(TARGET main PROPERTY RUNTIME_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}>")
|