# Linking static libraries with custom object files

**URL:** <https://discourse.cmake.org/t/linking-static-libraries-with-custom-object-files/3453>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [June 1, 2021, 7:24am UTC](https://discourse.cmake.org/t/linking-static-libraries-with-custom-object-files/3453 "2021-06-01T07:24:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![serge](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@serge](https://discourse.cmake.org/u/serge)\
**Post date:** [June 1, 2021, 7:24am UTC](https://discourse.cmake.org/t/linking-static-libraries-with-custom-object-files/3453/1 "2021-06-01T07:24:36Z")

</div>

Hi everyone, something that looks basic and as a new one to cmake, I may miss:

Windows environment:  
I have two directories A and B  
B is a subdirectory of A  
Both have their own cmake file:

A:  
add\_subdirectory(B)  
add\_library(lib-a a.cpp)  
target\_link\_libraries(lib-a $\<TARGET\_OBJECTS:lib-b)

B:  
add\_library(lib-b OBJECT b1.cpp b2.obj)

and this is it. Very simple case  
Remarks:  
. lib-a is a static library that needs to be linked with lib-b  
. lib-b has b1.cpp and b2.obj as source files because b2.obj is generated by a custom rule.  
. lib-b is defined as OBJECT and not STATIC since there is no way to tell cmake to link it to lib-a.  
You even can’t push the hard code library file path into target\_link\_library; OBJECT is the way to go.  
. In the link command that builds lib-a I can see that b1.obj is included but not b2.obj just as if $\<TARGET\_OBJECTS:lib-b\> includes b1.obj but does not include b2.obj such that finally lib-a is incomplete

There does not seem to be a way to pass a variable from directory B to directory A: this variable would  
include the custom object files and I could have added the list to lib-a’s sources.

Am I missing some basic mechanism of cmake since this seems a basic construct?

Thanks in advance for any suggestion

---

<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:** [June 1, 2021, 10:57am UTC](https://discourse.cmake.org/t/linking-static-libraries-with-custom-object-files/3453/2 "2021-06-01T10:57:47Z")

</div>

> [@serge](#):
>
> lib-b is defined as OBJECT and not STATIC since there is no way to tell cmake to link it to lib-a.

In CMake, static libraries do not “link” (though some linkers will check symbol availability, there’s no actual copying of symbols between libraries; only object files get collected).

> [@serge](#):
>
> In the link command that builds lib-a I can see that b1.obj is included but not b2.obj just as if $\<TARGET\_OBJECTS:lib-b\> includes b1.obj but does not include b2.obj such that finally lib-a is incomplete

I believe that `TARGET_OBJECTS` is derived from the compilation commands CMake generates and manual `.obj` files are not considered. I don’t know that CMake provides this abstraction right now. [This issue](https://gitlab.kitware.com/cmake/cmake/-/issues/21709) seems to be the one you’d be most interested in.

---

<div class="post-metadata">

**Author:** ![serge](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/5f8ce5/32.png) [@serge](https://discourse.cmake.org/u/serge)\
**Post date:** [June 1, 2021, 11:18am UTC](https://discourse.cmake.org/t/linking-static-libraries-with-custom-object-files/3453/3 "2021-06-01T11:18:25Z")

</div>

Thanks Ben for your answer.

> [@ben.boeckel](#):
>
> I believe that `TARGET_OBJECTS` is derived from the compilation commands CMake generates and manual `.obj` files are not considered.

And yes I can see that but then what is the recommended method to link those object files. This is not really a corner case.  
I could finally solve this problem by defining a custom property on a target that lists all those object files. This is because you can not pass some global list variable from cmake to cmake file. It is cumbersome and ugly - is there some kind of nicer way to do that?

---

<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:** [June 1, 2021, 1:40pm UTC](https://discourse.cmake.org/t/linking-static-libraries-with-custom-object-files/3453/4 "2021-06-01T13:40:14Z")

</div>

I’m not sure if there’s a _supported_ way of doing it, but you could probably poke around the properties that get queried to see if you can inject you object anywhere. Listing it as an `INTERFACE` source file might work?

---

<div class="post-metadata">

**Author:** ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)\
**Post date:** [June 1, 2021, 8:28pm UTC](https://discourse.cmake.org/t/linking-static-libraries-with-custom-object-files/3453/5 "2021-06-01T20:28:45Z")

</div>

Yes, you can list the objs as sources.

If this is crossing project ‘install’ barrier, you can also install the OBJS and recreate an `INTERFACE` target with those objs as sources.
