Problems with interactions between $<TARGET_OBJECTS:lib> and $<$<CONFIG:Debug>:src.c>

I’m combining libraries in a cmake-based project using:

    add_library(combined)
    target_sources(combined PRIVATE $<TARGET_OBJECTS:lib1> $<TARGET_OBJECTS:lib2>)

This in most cases works fine, but some of the source files for lib1 and lib2 have been speicified using $<$<CONFIG:Debug>:src.c>, and when using one of the vc++ generators in command-line-issued release builds, the conditional source files seem to get included in the target_sources of the combined lib, although they are (as is correct) not built. That leads to a failed build.

Are there ways around this problem?

This gets stranger. Reversing the order of $<TARGET_OBJECTS:lib1> and $<TARGET_OBJECTS:lib2> makes the build succeed. I’ve created a public gitlab repo showing this in the simplest form I can: Glidos Public / cmake-target-objects-failure · GitLab. The head of master shows the failure. One commit back is the succeeding version. It’s an entirely trivial example. I’ve provided a build.bat file so there is no uncertainty in the commands being used.

Have you tried making lib1 and lib2 into OBJECT libraries? It was OBJECT libraries for which TARGET_OBJECTS was originally intended. I have no idea whether it can help, but it’s something I’d try if this was happening to me.

I didn’t try making lib1 and lib2 into OBJECT libraries. I will give that a try just to find out whether it makes a difference, but it may be not ideal because I have other builds where I need the individual libraries and I’d rather not add more conditions to handle the different cases.

Anyway, I do now have a workaround. If I split up the definitions like this:

add_library(combined)
target_sources(combined PRIVATE $<TARGET_OBJECTS:lib1>)
target_sources(combined PRIVATE $<TARGET_OBJECTS:lib2>)

both orders in which I add the two libraries then work. If you’re wondering why I care about being able to use both orderings, it’s because this is a very simplified version of my actual project. In my actual project there are around 400 libraries, and I derive the list programatically.