# unable to get target location

**URL:** https://discourse.cmake.org/t/unable-to-get-target-location/797
**Category:** Code
**Created:** [March 12, 2020, 12:51pm UTC](https://discourse.cmake.org/t/unable-to-get-target-location/797 "2020-03-12T12:51:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [March 12, 2020, 12:51pm UTC](https://discourse.cmake.org/t/unable-to-get-target-location/797/1 "2020-03-12T12:51:20Z")

</div>

I am getting a location not found for my custom target. Here is the code snippet:

```cmake
cmake_minimum_required( VERSION 3.17 )
project(Debug)

add_custom_target(tgt ALL)

get_target_property( TGT_LOCATION tgt LOCATION )

message( "loc: ${TGT_LOCATION}" )

```

result:

```auto
loc: TGT_LOCATION-NOTFOUND
Scanning dependencies of target tgt
Built target tgt

```

`LOCATION` is the Read-only location of a target on disk. For a non-imported target, this property is provided for compatibility with CMake 2.4 and below.

what am I missing here?

---

<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 12, 2020, 8:14pm UTC](https://discourse.cmake.org/t/unable-to-get-target-location/797/2 "2020-03-12T20:14:48Z")

</div>

A custom target has no build output, so there is nothing to get the location of. If `tgt` was a library or executable, it would work okay. That said, it would be rare to need the `LOCATION` target property. In the context of where you want to use it, if generator expressions are supported, the expression `$<TARGET_FILE:tgt>` is much more robust because it works for multi-config generators, whereas `LOCATION` doesn’t.

---

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [March 13, 2020, 10:52am UTC](https://discourse.cmake.org/t/unable-to-get-target-location/797/3 "2020-03-13T10:52:38Z")

</div>

I am still unsuccessful with this after applying `CMP0026`:

```cmake
cmake_minimum_required( VERSION 3.17 )
project(Debug)

cmake_policy(SET CMP0026 OLD)
add_custom_target(tgt ALL)

get_target_property( TGT_LOCATION tgt LOCATION )

message( "loc: ${TGT_LOCATION}" )

```

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [March 13, 2020, 10:56am UTC](https://discourse.cmake.org/t/unable-to-get-target-location/797/4 "2020-03-13T10:56:01Z")

</div>

As Craig said, you’re dealing with a custom target (added by `add_custom_target()`). These do _not_ have a location; the concept makes no sense for them.

What do you expect the location of your custom target to be? Based on that, there might be a different way to obtain such information.

---

<div class="post-metadata">

### Author: ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)
#### Post date: [March 13, 2020, 11:09am UTC](https://discourse.cmake.org/t/unable-to-get-target-location/797/5 "2020-03-13T11:09:39Z")

</div>

this is about a legacy code snippet for a build procedure I have limited knowledge on. I am trying to understand the location by observation, to make adjustments accordingly.

I do understand that the provided solution by Craig must be implemented afterwards.

The part that is confusing me here is the documentation:

> For a non-imported target, this property is provided for compatibility  
> with CMake 2.4 and below. It was meant to get the location of an  
> executable target’s output file for use in |add\_custom\_command|.

`tgt` is a non-imported target. It states that this property was used in CMake \<= 2.4 for custom commands. It implies that this property is automatically set in later versions.

> […] In CMake 2.6 and above  
> |add\_custom\_command| automatically recognizes a target name in its  
> `COMMAND` and `DEPENDS` options and computes the target location. In  
> CMake 2.8.4 and above |add\_custom\_command| recognizes  
> generator expressions \<cmake-generator-expressions(7)\>  
> to refer to target locations anywhere in the command.  
> Therefore this property is not needed for creating custom commands.

For CMake \>= 2.8.4 usage for this purpose is discouraged.

CMP0026 is marked deprecated for 3.17 but the old behaviour hasn’t been dropped yet.

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [March 13, 2020, 12:51pm UTC](https://discourse.cmake.org/t/unable-to-get-target-location/797/6 "2020-03-13T12:51:51Z")

</div>

Notice that even the documentation you quoted says “It was meant to get the location of an  
**executable** target’s output file” (emphasis mine). You have a _custom_ target, not an _executable_ target. Custom targets don’t have outputs, so they don’t have a location, and have never had one.

If your custom target produces something at a path on disk, you should use your knowledge of that path directly instead of trying to query it from the target. If that proves difficult due to scoping or knowledge-separation issues, you can set a custom property on the custom target which will store that path, and retrieve that property at the point you need to use it.
