How to use CMake generator expression $?

I cannot understand what I’m doing wrong. I’m always getting the string $ 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 $.

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

reference

How to use CMake generator expression $?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值