cmake config_error ai_generated true

CMake 错误:if 给定的参数:"VAR" "STREQUAL" "value" 指定了未知参数

CMake Error: if given arguments: "VAR" "STREQUAL" "value" Unknown arguments specified

ID: cmake/variable-not-defined-in-if

其他格式: JSON · Markdown 中文 · English
95%修复率
87%置信度
1证据数
2023-08-10首次发现

版本兼容性

版本状态引入弃用备注
CMake 3.10 active
CMake 3.16 active
CMake 3.22 active
CMake 3.27 active

根因分析

在 CMake if() 条件中使用了变量名但没有加 ${},导致 CMake 将其视为字面字符串而不是展开变量。

English

Using a variable name without ${} in a CMake if() condition, causing CMake to treat it as a literal string instead of expanding it.

generic

官方文档

https://cmake.org/cmake/help/latest/command/if.html

解决方案

  1. Use ${VAR} in the if() condition to expand the variable.
  2. Alternatively, use if(DEFINED VAR) to check if the variable is defined, then use ${VAR} in comparisons.

无效尝试

常见但无效的做法:

  1. Adding quotes around the variable: if("${VAR}" STREQUAL "value") 10% 失败

    Quoting the variable expansion is fine, but the real issue is forgetting ${}. This workaround actually works if the variable is defined, but it's a style issue.

  2. Removing the STREQUAL keyword and using if(VAR value) thinking it's a different syntax 95% 失败

    CMake does not support if(VAR value) as a comparison; it will either error or give unexpected results.

  3. Defining the variable with set(VAR "value") but still using bare VAR in if() 80% 失败

    Even if the variable is defined, bare VAR in if() checks if the literal string 'VAR' is defined, not the variable's value.