unable to get target location

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

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:

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?

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.

1 Like

I am still unsuccessful with this after applying CMP0026:

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}" )

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.

1 Like

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.

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.