commit 0e1cb50299d628f55d370ca0c4c4652367ee9005 Author: Luís Murta Date: Tue Aug 5 23:30:57 2025 +0100 CMake transitive LINK_LIBRARIES diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7194ea7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.cache +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..25f41e8 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,65 @@ +cmake_minimum_required(VERSION 3.12...4.1) +project(link-libraries) + +# Added in 4.1, enables transitive link libraries on LINK_LIBRARIES property +# https://cmake.org/cmake/help/v4.1/policy/CMP0189.html +# https://gitlab.kitware.com/cmake/cmake/-/issues/26709 +# https://gitlab.kitware.com/cmake/cmake/-/merge_requests/10391 +cmake_policy(SET CMP0189 NEW) + +add_library(shared1 SHARED dummy.cpp) +add_library(shared2 SHARED dummy.cpp) +add_library(shared3 SHARED dummy.cpp) +add_library(interface1 INTERFACE) +add_library(interface2 INTERFACE) +add_library(interface3 INTERFACE) +add_library(interface4 INTERFACE) +add_library(static1 STATIC dummy.cpp) +add_library(static2 STATIC dummy.cpp) + +add_library(lib1 STATIC dummy.cpp) +target_link_libraries( + lib1 + PUBLIC shared1 interface1 static1 + PRIVATE shared2 interface2 static2 + INTERFACE interface3) + +add_library(lib2 SHARED dummy.cpp) +target_link_libraries(lib2 shared3 interface3) + +add_executable(exe1 dummy.cpp) +target_link_libraries(exe1 PRIVATE lib1 lib2 interface4) + +add_executable(exe2 dummy.cpp) +target_link_libraries(exe2 lib1 lib2 interface4) + +get_target_property(lib1_LINK_LIBRARIES lib1 LINK_LIBRARIES) +get_target_property(lib1_INTERFACE_LINK_LIBRARIES lib1 INTERFACE_LINK_LIBRARIES) +message(STATUS "lib1_LINK_LIBRARIES: ${lib1_LINK_LIBRARIES}") +message(STATUS "lib1_INTERFACE_LINK: ${lib1_INTERFACE_LINK_LIBRARIES}") + +get_target_property(exe1_LINK_LIBRARIES exe1 LINK_LIBRARIES) +get_target_property(exe1_INTERFACE_LINK_LIBRARIES exe1 INTERFACE_LINK_LIBRARIES) +message(STATUS "exe1_LINK_LIBRARIES: ${exe1_LINK_LIBRARIES}") +message(STATUS "exe1_INTERFACE_LINK: ${exe1_INTERFACE_LINK_LIBRARIES}") + +get_target_property(exe2_LINK_LIBRARIES exe2 LINK_LIBRARIES) +get_target_property(exe2_INTERFACE_LINK_LIBRARIES exe1 INTERFACE_LINK_LIBRARIES) +message(STATUS "exe2_LINK_LIBRARIES: ${exe2_LINK_LIBRARIES}") +message(STATUS "exe2_INTERFACE_LINK: ${exe2_INTERFACE_LINK_LIBRARIES}") + +# https://stackoverflow.com/questions/51353110/how-do-i-output-the-result-of-a-generator-expression-in-cmake +file( + GENERATE + OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/lib1_link_libraries.txt + CONTENT "$") +file( + GENERATE + OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/exe1_link_libraries.txt + CONTENT "$") +file( + GENERATE + OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/exe2_link_libraries.txt + CONTENT "$") + +# set_property(GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1) diff --git a/README.md b/README.md new file mode 100644 index 0000000..5298732 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# CMake transitive LINK_LIBRARIES + +Small test to check if the target property LINK_LIBRARIES was transitive. It seems it was only added +as the result of the generator expression $ in CMake 4.1. diff --git a/dummy.cpp b/dummy.cpp new file mode 100644 index 0000000..e69de29 diff --git a/exe1_link_libraries.txt b/exe1_link_libraries.txt new file mode 100644 index 0000000..8ba879c --- /dev/null +++ b/exe1_link_libraries.txt @@ -0,0 +1 @@ +lib1;lib2;interface4;shared1;interface1;static1;shared2;interface2;static2;interface3;shared3;interface3 \ No newline at end of file diff --git a/exe2_link_libraries.txt b/exe2_link_libraries.txt new file mode 100644 index 0000000..8ba879c --- /dev/null +++ b/exe2_link_libraries.txt @@ -0,0 +1 @@ +lib1;lib2;interface4;shared1;interface1;static1;shared2;interface2;static2;interface3;shared3;interface3 \ No newline at end of file diff --git a/lib1_link_libraries.txt b/lib1_link_libraries.txt new file mode 100644 index 0000000..3f55fd9 --- /dev/null +++ b/lib1_link_libraries.txt @@ -0,0 +1 @@ +shared1;interface1;static1;shared2;interface2;static2 \ No newline at end of file