# What are imported and alias executables good for

**URL:** <https://discourse.cmake.org/t/what-are-imported-and-alias-executables-good-for/5315>\
**Category:** Code\
**Created:** [March 29, 2022, 10:22am UTC](https://discourse.cmake.org/t/what-are-imported-and-alias-executables-good-for/5315 "2022-03-29T10:22:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NeoCortex97](https://discourse.cmake.org/user_avatar/discourse.cmake.org/neocortex97/32/1599_2.png) [@NeoCortex97](https://discourse.cmake.org/u/NeoCortex97)\
**Post date:** [March 29, 2022, 10:22am UTC](https://discourse.cmake.org/t/what-are-imported-and-alias-executables-good-for/5315/1 "2022-03-29T10:22:46Z")

</div>

I have read the docs for executables, but I do not really understand when I should use imported executables or aliases.  
[https://cmake.org/cmake/help/latest/command/add\_executable.html](https://cmake.org/cmake/help/latest/command/add_executable.html)  
This is the link that I was reading. But maybe I am missing the practical experience, or I am not good enough in english to understand it.

I hope you could explain it to me. I am trying to improve my CMake code and write a generator to give me a clean starting point for a project.

As I understood, imported executables could be used to make custom commands more readable, but how could that improve readability?

And as I understand alias could be used to give a shorter name to a target so your code could be more readable if many operations are performed on a target.  
But I don’t understand when it makes sense to use these things and how to use them.

I could simply get some examples from google, but that would not explain why and when to use them.

And feel free to tell me, if my questions are stupid.

---

<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:43pm UTC](https://discourse.cmake.org/t/what-are-imported-and-alias-executables-good-for/5315/2 "2022-03-29T13:43:06Z")

</div>

> [@NeoCortex97](#):
>
> As I understood, imported executables could be used to make custom commands more readable, but how could that improve readability?

You can do `$<TARGET_FILE:SomePkg::Executable>` in the command instead of providing a variable with the path (CMake will make this relocatable while a variable generally needs manual logic).

> [@NeoCortex97](#):
>
> And as I understand alias could be used to give a shorter name to a target so your code could be more readable if many operations are performed on a target.  
> But I don’t understand when it makes sense to use these things and how to use them.

`ALIAS` targets are more useful to make the name of the target when exported available within the build as well. Basically, it lets you have one name for a target no matter where it is.

---

<div class="post-metadata">

**Author:** ![NeoCortex97](https://discourse.cmake.org/user_avatar/discourse.cmake.org/neocortex97/32/1599_2.png) [@NeoCortex97](https://discourse.cmake.org/u/NeoCortex97)\
**Post date:** [March 31, 2022, 8:19am UTC](https://discourse.cmake.org/t/what-are-imported-and-alias-executables-good-for/5315/3 "2022-03-31T08:19:56Z")

</div>

> [@ben.boeckel](#):
>
> You can do `$<TARGET_FILE:SomePkg::Executable>` in the command instead of providing a variable with the path (CMake will make this relocatable while a variable generally needs manual logic).

That is nice. Could someone point me to some usage example for that? I tried using it in a test, but was unable to get it working. Maybe some resources about generator expressions wold be good too, I might simply keep messing up the generator expressions.

> [@ben.boeckel](#):
>
> `ALIAS` targets are more useful to make the name of the target when exported available within the build as well. Basically, it lets you have one name for a target no matter where it is.

Sounds usefull. I might still need an example at a later point, but I think i get it atm.

---

<div class="post-metadata">

**Author:** ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Post date:** [March 31, 2022, 12:13pm UTC](https://discourse.cmake.org/t/what-are-imported-and-alias-executables-good-for/5315/4 "2022-03-31T12:13:47Z")

</div>

The other useful property of alias targets is that they’re not modifiable or installable – they’re basically const aliases. So it’s useful to always reference an alias when you’re linking to targets defined in one place from other places in your code, because you cannot accidentally change anything about the underlying target being aliased.
