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

- **ID:** `cmake/variable-not-defined-in-if`
- **领域:** cmake
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.10 | active | — | — |
| CMake 3.16 | active | — | — |
| CMake 3.22 | active | — | — |
| CMake 3.27 | active | — | — |

## 解决方案

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

## 无效尝试

- **Adding quotes around the variable: if("${VAR}" STREQUAL "value")** — 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. (10% 失败率)
- **Removing the STREQUAL keyword and using if(VAR value) thinking it's a different syntax** — CMake does not support if(VAR value) as a comparison; it will either error or give unexpected results. (95% 失败率)
- **Defining the variable with set(VAR "value") but still using bare VAR in if()** — Even if the variable is defined, bare VAR in if() checks if the literal string 'VAR' is defined, not the variable's value. (80% 失败率)
