CMake生成器表达式未评估 [英] CMake generator expression is not evaluated

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

问题描述

我不明白自己在做什么错。
我总是得到字符串 $< TARGET_FILE:tgt1> 而不是库的路径。



我已经创建了一个虚拟项目。



这是我的根CMakeLists.txt

  cmake_minimum_required(VERSION 3.0)#也尝试了2.8,结果相同
set(PROJECT_NAME CMP0026)

add_subdirectory(src)

set(TGT_PATH $< TARGET_FILE:tgt1>)
消息(STATUS $ {TGT_PATH})

这是我的src / CMakeLists.txt

  add_library(tgt1 ac)

文件 ac 已创建且为空



我尝试了以下生成器:Visual Studio 2013 Win64,Ninja和MingW Makefile。我已使用Android工具链作为最后两个工具,可从此处下载p>

我希望最后一个 message(STATUS 命令将显示库的完整路径。但是,所有变体都将打印字符串 $< TARGET_FILE:tgt1>

解决方案

生成器表达式不是在配置时评估(当CMake解析CMakeList时,执行 add_target() message()等命令)此时,生成器表达式只是一个文字字符串-字符 $ 后跟< ,然后是 T ,然后...



生成器表达式的评估发生在 generate 时间(即为什么将它们称为生成器表达式。生成时间发生在所有CMake代码都经过解析和处理之后,并且CMake开始对其中的数据进行操作以生成构建系统文件。只有 then 具备评估生成器表达式所需的所有信息。



因此,您只能对生成器中发生的事情真正使用生成器表达式时间或更高的时间(例如构建时间)。一个人为的例子是:

  add_custom_target(
GenexDemo
COMMAND $ {CMAKE_COMMAND} -E echo $& VERBATIM

在配置时时间,CMake会将文字字符串 $< TARGET_FILE:tgt1> 记录为 COMMAND 的参数。然后在生成时(当每个配置已知 tgt1 的位置并且保证不再更改时),它将用它代替生成器表达式。


I cannot understand what I'm doing wrong. I'm always getting the string "$<TARGET_FILE:tgt1>" instead of the path to the library.

I've created the dummy project.

Here is my root CMakeLists.txt

cmake_minimum_required (VERSION 3.0) # also tried 2.8 with the same result
set(PROJECT_NAME CMP0026)

add_subdirectory(src)

set(TGT_PATH $<TARGET_FILE:tgt1>)
message(STATUS "${TGT_PATH}")

Here is my src/CMakeLists.txt

add_library(tgt1 a.c)

File a.c is created and is empty

I've tried the following generators: Visual Studio 2013 Win64, Ninja and MingW Makefile. I've used Android toolchain for the last two, downloaded from here

I expect that the last message(STATUS command would print full path to the library. However, all variants print the string $<TARGET_FILE:tgt1>.

解决方案

Generator expressions are not evaluated at configure time (when CMake is parsing CMakeLists, executing commands like add_target() or message() etc.). At this time, a generator expression is just a literal string - the character $ followed by <, then T, then ...

Evaluation of generator expressions happens at generate time (that's why they are called "generator expressions"). Generate time occurs after all CMake code is parsed and processed, and CMake is starting to act on the data therein to produce buildsystem files. Only then does it have all the information necessary to evaluate generator expressions.

So you can only really use generator expressions for things which occur at generate time or later (such as build time). A contrived example would be this:

add_custom_target(
  GenexDemo
  COMMAND ${CMAKE_COMMAND} -E echo "$<TARGET_FILE:tgt1>"
  VERBATIM
)

At configure time, CMake will record the literal string $<TARGET_FILE:tgt1> as the argument of COMMAND. Then at generate time (when the location of tgt1 is known for each configuration and guaranteed not to change any more), it will substitute it for the generator expression.

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

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