CMake Generator表达式未求值 [英] CMake generator expression is not being evaluated

查看:71
本文介绍了CMake Generator表达式未求值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下警告:

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.

这是以下行的结果:

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

但是 TARGET_FILE 未得到评估

path to executable: $<TARGET_FILE:my_exe>

我正在使用CMake 3.4,并添加了 cmake_minimum_required(版本3.4)到我的 CMakeLists.txt ,所以我在做什么错了?

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

推荐答案

在生成器表达式存储在 configuration 阶段(执行相应的CMake命令时)的同时,生成器表达式的
evaluation 构建阶段

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

这就是为什么 message()命令以非解引用形式打印生成器表达式的原因:生成器表达式表示的值在此阶段尚不明确

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

此外,CMake不会自行取消引用生成器表达式。而是在生成文件中生成适当的字符串,然后由 build实用程序 make Visual Studio 等。)

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.).

请注意,并非每个 CMake 命令都接受 生成器表达式生成器表达式的每种可能用法在文档(用于特定命令)。此外,不同的CMake 命令流不同的选项对于使用生成器表达式具有不同的政策。

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.

例如,命令流

add_test(NAME <name> COMMAND <executable>)

accepts 生成器表达式,用于 COMMAND 选项,

accepts generator expressions for COMMAND option,

但命令流

add_test(<name> <executable>)

没有!

另一个政策差异示例:

install(DIRECTORY <dir> DESTINATION <dest>)

在此命令中,流生成器表达式用于 DESTINATION ,但不提供 DIRECTORY 选项。

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

再次,仔细阅读文档

这篇关于CMake Generator表达式未求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆