cmake build_error ai_generated true

CMake 错误:计算生成器表达式时出错: $<TARGET_FILE:mylib> 表达式不以生成器表达式标记开头。

CMake Error: Error evaluating generator expression: $<TARGET_FILE:mylib> The expression does not start with a generator expression token.

ID: cmake/generator-expression-missing-dollar

其他格式: JSON · Markdown 中文 · English
85%修复率
84%置信度
1证据数
2024-01-10首次发现

版本兼容性

版本状态引入弃用备注
cmake 3.18 active
cmake 3.22 active
cmake 3.26 active
cmake 3.30 active

根因分析

在支持生成器表达式的上下文之外使用了生成器表达式(例如在普通的 set() 命令或注释中),或者表达式格式错误(缺少开头的 $)。

English

A generator expression was used outside of a context that supports them (e.g., in a regular set() command or a comment), or the expression is malformed (missing leading $).

generic

官方文档

https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html

解决方案

  1. 将生成器表达式移到支持它的目标属性或命令中,例如 target_compile_definitions(mylib PRIVATE "$<TARGET_FILE:mylib>")
  2. 如果在文件写入中使用,请使用 file(GENERATE ...) 配合表达式:file(GENERATE OUTPUT output.txt CONTENT "$<TARGET_FILE:mylib>")

无效尝试

常见但无效的做法:

  1. 50% 失败

    Generator expressions are not shell variables; escaping breaks the syntax.

  2. 70% 失败

    message() does not evaluate generator expressions at configure time; use file(GENERATE) instead.