CMake generator expression is not being evaluated

2020-07-06 05:42发布

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?

标签: cmake
2条回答
劫难
2楼-- · 2020-07-06 06:35

Here is a quick and easy way to print the value of a generator expression:

add_custom_target(print
        ${CMAKE_COMMAND} -E echo $<1:hello> $<0:world>
    )

In this example, if you run cmake . and then make print, you will see "hello" (without the quotation marks) in the output.

However, if you just use message($<1:hello> $<0:world>), you will see "$<1:hello> $<0:world>" as output (again, without the quotation marks).

查看更多
爷的心禁止访问
3楼-- · 2020-07-06 06:40

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.

查看更多
登录 后发表回答