# Usage of object libraries

**URL:** https://discourse.cmake.org/t/usage-of-object-libraries/12764
**Category:** Code
**Created:** [October 11, 2024, 3:50pm UTC](https://discourse.cmake.org/t/usage-of-object-libraries/12764 "2024-10-11T15:50:18Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![pevsonic](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pevsonic/32/5012_2.png) [@pevsonic](https://discourse.cmake.org/u/pevsonic)
#### Post date: [October 11, 2024, 3:50pm UTC](https://discourse.cmake.org/t/usage-of-object-libraries/12764/1 "2024-10-11T15:50:18Z")

</div>

Ive got a project Im converting over to CMake. One of the more unusual things that it involves are some custom binary config data objects it creates from object files. In the old make project it would take the .o’s, objcopy them then create a single binary from them using specific offsets and add a CRC.

Im trying to work out how I can do this using CMake without spiralling into a hole of weird complexity. I’ve got as far as figuring out I can use an  
add\_library(target OBJECT …)

which to begin with seems to get me started. However, next step is running objcopy on each .o in turn to create binary data files.

Initially I tried using add\_custom\_command() but it appears you can’t use “$\<TARGET\_OBJECTS:objlib\>” as a dependency… As a bodge I’ve running a foreach on the list of sources, using a string regex replace to generate a .o name then adding it as a dependency in add\_custom\_command()… This seems to work, but now im stuck as I can’t work out how to get the path of the .o file where it was created. in the custom command it seems I can now see “$\<TARGET\_OBJECTS:objlib\>” but it’s a list of all the obj paths, not just the one I want…

Anyone got any ideas on where to go from here? I feel like I might be completely misunderstanding cmake’s design intent… Is it really this hard?

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [October 15, 2024, 10:03pm UTC](https://discourse.cmake.org/t/usage-of-object-libraries/12764/2 "2024-10-15T22:03:15Z")

</div>

That GenEx should be perfectly fine as dependency in a custom command.

As the content of GenEx is not available during configuration, you need to postpone your evaluation to build time.  
I suggest to write the file list via `file(GENERATE)` into a file first. Then use a (CMake) script as custom command. In that script read that file list and do your obcopy calls in a for-loop.
