# execute\_process() vs add\_custom\_command()

**URL:** https://discourse.cmake.org/t/execute-process-vs-add-custom-command/3286
**Category:** Code
**Created:** [May 10, 2021, 1:23pm UTC](https://discourse.cmake.org/t/execute-process-vs-add-custom-command/3286 "2021-05-10T13:23:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![KUGA2](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kuga2/32/4968_2.png) [@KUGA2](https://discourse.cmake.org/u/KUGA2)
#### Post date: [May 10, 2021, 1:23pm UTC](https://discourse.cmake.org/t/execute-process-vs-add-custom-command/3286/1 "2021-05-10T13:23:58Z")

</div>

Simple question:  
When should I use one of them?

Or a little biased:  
Why/When should I use execute\_process() over add\_custom\_command()?

The difference between those two is, that execute\_process() works at configure time (therefore will be executed with every cmake configure) and add\_custom\_command() works at build time (and is only executed when the dependencies change)

I only need add\_custom\_command(), but beginners tend to use execute\_process() since it is easier to understand (and easier to find via doc/google), but dependencies are usually omitted.

---

<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: [May 10, 2021, 1:33pm UTC](https://discourse.cmake.org/t/execute-process-vs-add-custom-command/3286/2 "2021-05-10T13:33:21Z")

</div>

It depends 🙂 . If it’s something that should happen automatically during the build, then you definitely want `add_custom_command`. If you’re running a program to get the default value or other system introspection (say, `numpy`'s header directory or available memory), this is `execute_process`'s job. If you’re generating sources, then you usually want `add_custom_command`, but if the generation is a combination of:

- expensive
- rarely changes
- hard to get dependencies right

then `execute_process` may work better.

Both have their place, but choosing one over the other requires knowing more about the situation in which it is needed.

---

<div class="post-metadata">

### Author: ![YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ymba9g8j9cjp0wloqf5y/32/1244_2.png) [@YMba9g8j9CJp0wLoQf5y](https://discourse.cmake.org/u/YMba9g8j9CJp0wLoQf5y)
#### Post date: [May 10, 2021, 1:48pm UTC](https://discourse.cmake.org/t/execute-process-vs-add-custom-command/3286/3 "2021-05-10T13:48:03Z")

</div>

I really don’t like using execute\_process unless I have to. Because execute\_process is done during the **configure stage** of cmake. To me a very quick configure stage is really important. Adding too much logic to your configure step slows down iteration speed which is crucial for rapid development/testing. Also execute\_process tends to be slower on Windows ([Why is cmake on Linux so much faster than on Windows?](https://discourse.cmake.org/t/why-is-cmake-on-linux-so-much-faster-than-on-windows/1918)) depending on what you are doing.

I like using add\_custom\_command since it happens during the **build step** and can be parallelized. Even if your custom task is single threaded the build can still potentially be running other tasks and whatnot.

Overall my opinion is:  
1.) Make sure you really need to do this in the first place. Get another person’s opinion.  
2.) **Document** what you are doing. Make sure other people understand what your custom build task is doing.  
3.) Choose add\_custom\_command over execute\_process **if possible**
