Skip to content

TARGET_OBJECTS generator expression doesn't add sources to an OBJECT library

The documentation states:

... other targets created by add_library() or add_executable() may reference the objects using an expression of the form $<TARGET_OBJECTS:objlib> as a source, where objlib is the object library name.

add_library(... $<TARGET_OBJECTS:objlib> ...)
add_executable(... $<TARGET_OBJECTS:objlib> ...)

This breaks down when trying to compose object libraries:

cmake_minimum_required(VERSION 3.21)

project(ObjectLibExample)

add_library(LibA.Obj OBJECT src/libA.cpp)
add_library(LibB.Obj OBJECT src/libB.cpp)

add_library(LibAB.Obj OBJECT $<TARGET_OBJECTS:LibA.Obj> $<TARGET_OBJECTS:LibB.Obj>)

add_library(LibAB SHARED $<TARGET_OBJECTS:LibAB.Obj>)

Here, LibAB.Obj should reference the objects of both LibA.Obj and LibB.Obj as sources, and when used to compile the shared library, the shared library should be built from these objects.

Instead, the Generate step fails with the error "No SOURCES given to target LibAB".

Image of error that occurs when attempting to compose object libraries. No sources given to target.

Edited by Alecto Irene Perez
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information