Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake generator expression is not being evaluated

Tags:

cmake

Due to the following warning:

CMake Error at test/CMakeLists.txt:29 (get_target_property):
  The LOCATION property may not be read from target "my_exe".  Use the
  target name directly with add_custom_command, or use the generator
  expression $<TARGET_FILE>, as appropriate.

which is the result from lines like this:

get_target_property(my_exe_path my_exe LOCATION)

Like recommended in the docs, I tried to use a generator expression like this:

add_executable(my_exe_path main.cpp)
message("path to executable: $<TARGET_FILE:my_exe_path>")

But TARGET_FILE is not being evaluated

path to executable: $<TARGET_FILE:my_exe>

I'm using CMake 3.4 and added cmake_minimum_required(VERSION 3.4) to my CMakeLists.txt so what am I doing wrong?

like image 947
frans Avatar asked Feb 29 '16 08:02

frans


1 Answers

While generator expression is stored at configuration stage (when corresponded CMake command is executed), evaluation of generator expressions is performed at build stage.

This is why message() command prints generator expression in non-dereferenced form: value denoted by the generator expression is not known at this stage.

Moreover, CMake never dereferences generator expressions by itself. Instead, it generates appropriate string in the build file, which is then interpreted by build utility (make, Visual Studio, etc.).

Note, that not every CMake command accepts generator expressions. Each possible usage of generator expressions is explicitely described in documentation for specific command. Moreover, different CMake command flows or different options have different policy about using of generator expressions.

For example, command flow

add_test(NAME <name> COMMAND <executable>)

accepts generator expressions for COMMAND option,

but command flow

add_test(<name> <executable>)

doesn't!

Another example of policies difference:

install(DIRECTORY <dir> DESTINATION <dest>)

In this command flow generator expressions are allowed for DESTINATION, but not for DIRECTORY option.

Again, read documentation carefully.

like image 91
Tsyvarev Avatar answered Sep 29 '22 09:09

Tsyvarev