# How to install a custom target ?

**URL:** https://discourse.cmake.org/t/how-to-install-a-custom-target/670
**Category:** Code
**Created:** [February 20, 2020, 6:12pm UTC](https://discourse.cmake.org/t/how-to-install-a-custom-target/670 "2020-02-20T18:12:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![yann](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/y/bc79bd/32.png) [@yann](https://discourse.cmake.org/u/yann)
#### Post date: [February 20, 2020, 6:12pm UTC](https://discourse.cmake.org/t/how-to-install-a-custom-target/670/1 "2020-02-20T18:12:52Z")

</div>

I am creating a target to generate a script using add\_custom\_target(). Now I want to install it, but:

- install(TARGETS) fails with “install TARGETS given target “script” which is not an executable, library, or module.” and I can’t find a way to tell cmake that this target has TYPE EXECUTABLE
- install(PROGRAMS) would allow me to specify “TYPE EXECUTABLE”, but does not appear to let me specify a file from build tree, only source tree

What did I miss ?

Moreover, the “correct way” to use add\_custom\_target() seems to be together with add\_custom\_command() if we want proper dependency handling. But then it seems I have to give different names to the “command” (which takes the output file name) and to the target. And then I’m at a loss as to how install(TARGETS) can be used at all: specifying the file name does not work as it is not a target name, and specifying the target name naturally does not work as it’s not the file name. So what ?

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [February 21, 2020, 1:48am UTC](https://discourse.cmake.org/t/how-to-install-a-custom-target/670/2 "2020-02-21T01:48:20Z")

</div>

> [@yann](#):
>
> install(PROGRAMS) would allow me to specify “TYPE EXECUTABLE”, but does not appear to let me specify a file from build tree, only source tree

What’s make you think so? The doc: [https://cmake.org/cmake/help/v3.13/command/install.html#programs](https://cmake.org/cmake/help/v3.13/command/install.html#programs)  
says:  
_File names **given as relative paths** are interpreted with respect to the current source directory_

I bet that if you provide absolute path you can install any file in the build tree.  
Did you try something like?  
`install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/yourscript.sh DESTINATION bin)`

> [@yann](#):
>
> And then I’m at a loss as to how install(TARGETS) can be used at all

I’m pretty sure you cannot use `install(TARGETS)` for custom target.

---

<div class="post-metadata">

### Author: ![yann](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/y/bc79bd/32.png) [@yann](https://discourse.cmake.org/u/yann)
#### Post date: [February 21, 2020, 3:13pm UTC](https://discourse.cmake.org/t/how-to-install-a-custom-target/670/3 "2020-02-21T15:13:41Z")

</div>

> [@erk](#):
>
> What’s make you think so? The doc: [https://cmake.org/cmake/help/v3.13/command/install.html#programs](https://cmake.org/cmake/help/v3.13/command/install.html#programs)  
> says:  
> _File names **given as relative paths** are interpreted with respect to the current source directory_
> 
> I bet that if you provide absolute path you can install any file in the build tree.  
> Did you try something like?  
> `install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/yourscript.sh DESTINATION bin)`

Right, this works, thanks. It would be useful to get this example in the doc, I’d say.  
Also, if we go with install(PROGRAMS), then better use “TYPE BIN” than “DESTINATION bin”

> [@erk](#):
>
> > [@yann](#):
> >
> > And then I’m at a loss as to how install(TARGETS) can be used at all
> 
> I’m pretty sure you cannot use `install(TARGETS)` for custom target.

That seems pretty counter-intuitive, to say the list. If this is so, it should surely be mentionned in the doc too.

Using install(PROGRAMS) still feels like a hack, and anyway it looks like we can’t use the “proper” add\_custom\_cammand() idiom.

---

<div class="post-metadata">

### Author: ![pranav.bhandarkar](https://discourse.cmake.org/user_avatar/discourse.cmake.org/pranav.bhandarkar/32/2930_2.png) [@pranav.bhandarkar](https://discourse.cmake.org/u/pranav.bhandarkar)
#### Post date: [November 18, 2022, 6:20pm UTC](https://discourse.cmake.org/t/how-to-install-a-custom-target/670/4 "2022-11-18T18:20:04Z")

</div>

> [@yann](#):
>
> That seems pretty counter-intuitive, to say the list. If this is so, it should surely be mentionned in the doc too.

I completely agree with you. I have spent the last 15 mins trying to wrestle with `install(TARGETS)` to install a custom target and failed. What was frustrating was I cotinued to try using `install(TARGETS)` over `install(PROGRAMS)` because of the following line in the docs

_This form is intended to install programs that are not targets, such as shell scripts. Use the `TARGETS` form to install targets built within the project._  
That is until I found this thread and @erk mentioned he is pretty sure custom targets cannot be installed with `install(TARGETS)`
