# does POST\_BUILD work with add\_custom\_target and Visual Studio?

**URL:** https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290
**Category:** Usage
**Tags:** gen:vs
**Created:** [March 24, 2022, 10:22pm UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290 "2022-03-24T22:22:52Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![dockd](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dockd/32/2293_2.png) [@dockd](https://discourse.cmake.org/u/dockd)
#### Post date: [March 24, 2022, 10:22pm UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/1 "2022-03-24T22:22:52Z")

</div>

I build a static library and then want to add two more static libs to it after it is built.  
The documentation for add\_custom\_command/“BUILD EVENTS” says

> The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute.

I have a command like

```auto
add_custom_command( TARGET something
    POST_BUILD
    COMMAND lib $<TARGET_FILE:something> $<TARGET_FILE:extraLib> $<TARGET_FILE:anotherLib>
  )

```

However, in practice, this always creates a post build command in Visual Studio, and the libs are linked every time, leading to a ton of warnings.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 25, 2022, 2:14am UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/2 "2022-03-25T02:14:44Z")

</div>

Maybe explain what you’re trying to achieve by adding other libraries to a static library. That seems strange, and likely isn’t what you’re ultimately needing to do, but without understanding the use case, it’s hard to tell.

---

<div class="post-metadata">

### Author: ![dockd](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dockd/32/2293_2.png) [@dockd](https://discourse.cmake.org/u/dockd)
#### Post date: [March 25, 2022, 4:20pm UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/3 "2022-03-25T16:20:07Z")

</div>

For better or worse, we want to deliver a single library to our clients. This library includes code we build as well as two third party libraries. So we build our lib, then add the other two after it has built.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 25, 2022, 10:01pm UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/4 "2022-03-25T22:01:40Z")

</div>

That is unlikely to work. You’d need to add the _contents_ of the other two libraries, not the libraries themselves. This has come a few times before, but I wasn’t able to find the past posts here in this form or the CMake issue tracker from a very quick scan. [This stackoverflow answer](https://stackoverflow.com/a/50031940/1938798) contains the general gist though.

Edit: [This issue](https://gitlab.kitware.com/cmake/cmake/-/issues/19224) is probably getting closer to discussing your scenario.

---

<div class="post-metadata">

### Author: ![dockd](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dockd/32/2293_2.png) [@dockd](https://discourse.cmake.org/u/dockd)
#### Post date: [March 26, 2022, 12:17am UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/5 "2022-03-26T00:17:27Z")

</div>

Those links describe what we’re trying to achieve. We want to merge our static lib with two other static libs.

The `add_custom_command` I posted does this. However, it _always_ executes even when “TARGET something” is already built.

Let me put it a different way: what if the custom command looks like this:

```auto
add_custom_command( TARGET something
    POST_BUILD
    COMMAND copy nul "foo.txt"
  )

```

I would expect to get a file named _foo.txt_ but only when building “something”. As it is, I get a file named _foo.txt_ even though “something” isn’t rebuilt. Am I misinterpreting the documentation for add\_custom\_target?

> If the target is already built, the command will not execute.

---

<div class="post-metadata">

### Author: ![fdk17](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/ea666f/32.png) [@fdk17](https://discourse.cmake.org/u/fdk17)
#### Post date: [March 26, 2022, 2:48am UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/6 "2022-03-26T02:48:20Z")

</div>

Targets from add\_custom\_target are always rebuilt. So adding a post build command should always run.

---

<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: [March 29, 2022, 1:14pm UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/7 "2022-03-29T13:14:47Z")

</div>

Can the linker grouping feature worked on by @marc.chevrier be used to add some `--whole-archive` flag or the like?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [March 29, 2022, 1:25pm UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/8 "2022-03-29T13:25:11Z")

</div>

The goal of the feature `WHOLE_ARCHIVE`, which must be used with `$<LINK_LIBRARY>` genex, is to load all objects of an archive file specified during the link of a shared library or an executable.

This is not at all usable to aggregate multiple archives into another one.

Another discussion for this problem in this [issue](https://gitlab.kitware.com/cmake/cmake/-/issues/22679).

---

<div class="post-metadata">

### Author: ![phetdam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/phetdam/32/4846_2.png) [@phetdam](https://discourse.cmake.org/u/phetdam)
#### Post date: [August 17, 2025, 11:54pm UTC](https://discourse.cmake.org/t/does-post-build-work-with-add-custom-target-and-visual-studio/5290/9 "2025-08-17T23:54:58Z")

</div>

The CMake documentation probably needs updating then. As quoted from [the docs](https://cmake.org/cmake/help/latest/command/add_custom_command.html#build-events):

> The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute.

Consider a simplified, generic case where target A has a `POST_BUILD` custom command to run. Using CMake 3.22, the above statement _is_ true for the Unix Makefiles generator at least (haven’t tried Ninja or others). E.g. I see that if target A is already built, the `POST_BUILD` custom command hung on A _won’t_ run, which matches what the documentation says.

However, using CMake 3.26 (from my Visual Studio installation), this is _not_ true for the Visual Studio 2022 generator I am using. If A is already built, the `POST_BUILD` custom command is _always_ re-run every time I call `cmake --build`. This causes something like renaming a generated file after building a target, which should only be done each time the original file is re-generated as part of the target build, to fail with the Visual Studio generator, as `cmake -E rename <src> <dest>` will of course error out if the `<src>` file doesn’t exist in the first place.

If this variation in behavior across generators is expected, then the documentation should be updated. If not, then the way the Visual Studio generators handle `POST_BUILD` custom commands is not in line with the documentation, so maybe that should be looked at.
