cmake build_error ai_generated true

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

Also available as: JSON · Markdown · 中文
85%Fix Rate
84%Confidence
1Evidence
2024-01-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
cmake 3.18 active
cmake 3.22 active
cmake 3.26 active
cmake 3.30 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 85% success Move the generator expression into a target property or a command that supports it, e.g., target_compile_definitions(mylib PRIVATE "$<TARGET_FILE:mylib>")
    Move the generator expression into a target property or a command that supports it, e.g., target_compile_definitions(mylib PRIVATE "$<TARGET_FILE:mylib>")
  2. 90% success If used in a file write, use file(GENERATE ...) with the expression: file(GENERATE OUTPUT output.txt CONTENT "$<TARGET_FILE:mylib>")
    If used in a file write, use file(GENERATE ...) with the expression: file(GENERATE OUTPUT output.txt CONTENT "$<TARGET_FILE:mylib>")

中文步骤

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

Dead Ends

Common approaches that don't work:

  1. 50% fail

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

  2. 70% fail

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