Multi-config generators fail to link per-config OBJECT library using a genex
EDIT: A simplified example appears in #25400 (comment 1443987)
Hi, I'm facing an oddity when using an object library as part of a generator expression in target_link_libraries. Below is the root CMakeLists.txt of an example I built.
cmake_minimum_required(VERSION 3.24)
project(cmake_bug VERSION 1.0
DESCRIPTION "Test project for possible cmake bug"
LANGUAGES C CXX
)
add_executable(${PROJECT_NAME}
test_runner_test_sample.c
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
c_test_lib
$<$<CONFIG:Release>:obj_lib>
)
add_subdirectory(unity)
add_subdirectory(c_static_lib)
add_subdirectory(c_test_lib)
add_subdirectory(obj_lib)
c_test_lib
is configured as a regular library, whereas obj_lib
is configured as an OBJECT library.
My expaction would be, that - for a Release configuration at least - obj_lib
is used when building the executable. Curiously though it isn't, even though the source file of obj_lib
is still getting compiled. This results in undefined references.
If obj_lib
is linked without the generator expression, this works as expected.
Also switching the approach from target_link_libraries to using $<$<CONFIG:Release>:$<TARGET_OBJECTS:obj_lib>>
in the add_executable
call works as expected and can be used as a workaround.
This behaviour has been tested using CMake 3.24.2, 3.27.7 and 3.28RC4.
Am I missing something in the generator expression, or why is this oddly misbehaving? Adding the same generator expression for a regular lib like $<$<CONFIG:Release>:c_test_lib>
works like that too, so I'm not sure what needs to be changed.