# Need to build twice to copy all dependencies in post build step

**URL:** https://discourse.cmake.org/t/need-to-build-twice-to-copy-all-dependencies-in-post-build-step/13785
**Category:** Usage
**Created:** [March 20, 2025, 3:44pm UTC](https://discourse.cmake.org/t/need-to-build-twice-to-copy-all-dependencies-in-post-build-step/13785 "2025-03-20T15:44:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![cen](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/ce7236/32.png) [@cen](https://discourse.cmake.org/u/cen)
#### Post date: [March 20, 2025, 3:44pm UTC](https://discourse.cmake.org/t/need-to-build-twice-to-copy-all-dependencies-in-post-build-step/13785/1 "2025-03-20T15:44:51Z")

</div>

I have a root cmake project consisting of two subprojects, one is a library, the other are unit tests. Unit tests link to the library.

On Windows, I need to copy all dependend .dll-s for shared builds next to the unit test .exe for it to be able to run.

I do this with the following logic seen here:

> <https://github.com/stachenov/quazip/blob/master/qztest/CMakeLists.txt#L45>

Since this is a Qt project, I first run windeployqt which is the recommended way to gather all necessary Qt dependencies.  
Then, three remaining dependencies need to be copied, zlib, bz2 and the tested library itself. I do that with custom command and $\<TARGET\_RUNTIME\_DLLS:qztest\> (it was incredibly hard to track this down in the first place).

This _almost_ works with a bizarre problem that bz2 is only copied if I run cmake build _a second time_. What could possibly cause this? Some kind of a race condition? I am all out of ideas.

Here is the workaround 🙂

> <https://github.com/stachenov/quazip/blob/master/.github/workflows/reusable-win.yml#L94>

---

<div class="post-metadata">

### Author: ![LegalizeAdulthood](https://discourse.cmake.org/user_avatar/discourse.cmake.org/legalizeadulthood/32/5282_2.png) [@LegalizeAdulthood](https://discourse.cmake.org/u/LegalizeAdulthood)
#### Post date: [March 20, 2025, 9:22pm UTC](https://discourse.cmake.org/t/need-to-build-twice-to-copy-all-dependencies-in-post-build-step/13785/2 "2025-03-20T21:22:49Z")

</div>

> [@cen](#):
>
> This _almost_ works with a bizarre problem that bz2 is only copied if I run cmake build _a second time_. What could possibly cause this? Some kind of a race condition?

This sounds to me like a missing dependency relationship.

> [@](#):
>
> windeployqt(qztest)

This looks to me like it should be part of your post-build script.

---

<div class="post-metadata">

### Author: ![cen](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/ce7236/32.png) [@cen](https://discourse.cmake.org/u/cen)
#### Post date: [March 20, 2025, 10:21pm UTC](https://discourse.cmake.org/t/need-to-build-twice-to-copy-all-dependencies-in-post-build-step/13785/3 "2025-03-20T22:21:49Z")

</div>

It is part of the post deploy, defined in cmake/windeployqt.cmake

Now that you mention it, it does seem that bzip is not added into target libraries the same way as zlib is so there might be something about that. I’ll look into it. I think I was too surprised about the fact that it was copied on second build to check if there are any differences from other dependencies that work on first try.

---

<div class="post-metadata">

### Author: ![LegalizeAdulthood](https://discourse.cmake.org/user_avatar/discourse.cmake.org/legalizeadulthood/32/5282_2.png) [@LegalizeAdulthood](https://discourse.cmake.org/u/LegalizeAdulthood)
#### Post date: [March 21, 2025, 3:29pm UTC](https://discourse.cmake.org/t/need-to-build-twice-to-copy-all-dependencies-in-post-build-step/13785/4 "2025-03-21T15:29:02Z")

</div>

Qt is, and always has been, a bit of a mess. Did you try asking in a Qt forum?
