# Linking a shared library using only objects from an object library

**URL:** https://discourse.cmake.org/t/linking-a-shared-library-using-only-objects-from-an-object-library/8430
**Category:** Usage
**Created:** [June 29, 2023, 12:58pm UTC](https://discourse.cmake.org/t/linking-a-shared-library-using-only-objects-from-an-object-library/8430 "2023-06-29T12:58:07Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ton](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/t/e0b2c6/32.png) [@ton](https://discourse.cmake.org/u/ton)
#### Post date: [June 29, 2023, 12:58pm UTC](https://discourse.cmake.org/t/linking-a-shared-library-using-only-objects-from-an-object-library/8430/1 "2023-06-29T12:58:07Z")

</div>

I am following the instructions from the manual to correctly propagate object files from an object library and usage dependencies according to: [target\_link\_libraries — CMake 3.27.0-rc3 Documentation](https://cmake.org/cmake/help/latest/command/target_link_libraries.html#linking-object-libraries-via-target-objects)

So I have some CMake logic along the following lines:

```auto
add_library(foo OBJECT)
target_sources(foo ...)

add_library(foo-interface INTERFACE)
target_link_libraries(foo-interface INTERFACE foo $<TARGET_OBJECTS:foo>)

add_library(foo-shared SHARED)
target_link_libraries(foo-shared PUBLIC foo-interface)

```

This does not work, since CMake complains about the library `foo-shared` being added without having any sources associated with it. It can be solved by specifying `target_sources()` separately but it sort of defeats the purpose of defining the targets objects on the intermediate interface library. Am I missing something here or is this some missing corner-case or feature?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 1, 2023, 9:39pm UTC](https://discourse.cmake.org/t/linking-a-shared-library-using-only-objects-from-an-object-library/8430/2 "2023-07-01T21:39:50Z")

</div>

> [@ton](#):
>
> `target_link_libraries(foo-interface INTERFACE foo $<TARGET_OBJECTS:foo>)`

Target objects should be in the _sources_ of `foo-interface`.

> [@ton](#):
>
> This does not work, since CMake complains about the library `foo-shared` being added without having any sources associated with it.

You should be able to add a `dummy.c` file or something to it. It’s mainly needed to know what compiler frontend to use for the linking of the library.
