# How get get the path of the installed executable?

**URL:** https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054
**Category:** Usage
**Created:** [September 8, 2021, 1:39pm UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054 "2021-09-08T13:39:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Ronald](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@Ronald](https://discourse.cmake.org/u/Ronald)
#### Post date: [September 8, 2021, 1:39pm UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054/1 "2021-09-08T13:39:05Z")

</div>

On Linux for item `Exec` of `.desktop`, it needs the path of the executable?  
Is there any better way than manully constructing as below?

```cmake
set(Item_Exec ${CMAKE_INSTALL_FULL_BINDIR}>/<target_name>)

```

---

<div class="post-metadata">

### Author: ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)
#### Post date: [September 8, 2021, 3:04pm UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054/2 "2021-09-08T15:04:33Z")

</div>

You can use `file(GENERATE ...)` and use a generator expression.

---

<div class="post-metadata">

### Author: ![Ronald](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@Ronald](https://discourse.cmake.org/u/Ronald)
#### Post date: [September 8, 2021, 3:43pm UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054/3 "2021-09-08T15:43:16Z")

</div>

`file(GENERATE ...)` generates a file, but I want to save it to a variable instead.

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [September 8, 2021, 3:44pm UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054/4 "2021-09-08T15:44:27Z")

</div>

You won’t be able to save it to a variable, because the output path is not known until generate-time, after `CMakeLists.txt` has already been processed.

---

<div class="post-metadata">

### Author: ![Ronald](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@Ronald](https://discourse.cmake.org/u/Ronald)
#### Post date: [September 8, 2021, 4:02pm UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054/5 "2021-09-08T16:02:15Z")

</div>

So the best way is

```cmake
set(Item_Exec ${CMAKE_INSTALL_FULL_BINDIR}>/<target_name>)

```

right?

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [September 8, 2021, 5:28pm UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054/6 "2021-09-08T17:28:58Z")

</div>

> [@Ronald](#):
>
> `CMAKE_INSTALL_FULL_BINDIR`

In general you can’t deduce the install location of binaries/libraries from CMake. As Kyle mentioned above that variable could hold generator expressions which have to be evaluated after CMake script execution to resolve to a valid path. In addition the `cmake --install` command offers the `--prefix` flag that allows changing the install location without re-executing CMake.

Under certain conditions ( GNUInstallDirs being used ) your solution might be sufficient, but it won’t be robust.

As far as .desktop files are considered ( [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html) ) you can just provide the name of the executable and allow the OS to search the environments PATH variable to find the program.

---

<div class="post-metadata">

### Author: ![Ronald](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/ea666f/32.png) [@Ronald](https://discourse.cmake.org/u/Ronald)
#### Post date: [September 9, 2021, 3:15am UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054/7 "2021-09-09T03:15:28Z")

</div>

> [@robert.maynard](#):
>
> As far as .desktop files are considered ( [Desktop Entry Specification](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html) ) you can just provide the name of the executable and allow the OS to search the environments PATH variable to find the program.

But the executable is in `/opt/myapp/bin/`, the default environment variable PATH doesn’t help.

---

<div class="post-metadata">

### Author: ![Bret\_Brown](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bret_brown/32/668_2.png) [@Bret\_Brown](https://discourse.cmake.org/u/Bret_Brown)
#### Post date: [September 12, 2021, 2:18am UTC](https://discourse.cmake.org/t/how-get-get-the-path-of-the-installed-executable/4054/8 "2021-09-12T02:18:37Z")

</div>

I don’t have enough information to be definitive here, but one of two scenarios seems likely.

**Scenario: You have standards or control over the destination system**

In that case, maybe you just hardcode `/opt/myapp/bin/` into `*.desktop` files in your package and move on.

**Scenario: You have inconsistent or incomplete information about how the package is installed**

Have you considered using a template file instead of a proper `*.desktop` file and use your packaging systems package-install-time hooks to generate (via `sed` perhaps?) a `*.desktop` file that knows the actual path as installed on the actual system? As stated above, `CMake` doesn’t actually know about even `/opt/myapp` necessarily. Many packaging systems don’t even manipulate a directory like `/opt/myapp` when CMake is in the picture, instead they install into a staging directory and then package that up. To be sure you’d know about the `/opt/myapp` path, you might need to inject that info into your `*.desktop` file much later in the process… for instance when the actual package installation is in progress.
