cmake
config_error
ai_generated
true
CMake Error: if given arguments: "VAR" "STREQUAL" "value" Unknown arguments specified
ID: cmake/variable-not-defined-in-if
95%Fix Rate
87%Confidence
1Evidence
2023-08-10First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| CMake 3.10 | active | — | — | — |
| CMake 3.16 | active | — | — | — |
| CMake 3.22 | active | — | — | — |
| CMake 3.27 | active | — | — | — |
Root Cause
Using a variable name without ${} in a CMake if() condition, causing CMake to treat it as a literal string instead of expanding it.
generic中文
在 CMake if() 条件中使用了变量名但没有加 ${},导致 CMake 将其视为字面字符串而不是展开变量。
Official Documentation
https://cmake.org/cmake/help/latest/command/if.htmlWorkarounds
-
95% success Use ${VAR} in the if() condition to expand the variable.
Use ${VAR} in the if() condition to expand the variable. -
90% success Alternatively, use if(DEFINED VAR) to check if the variable is defined, then use ${VAR} in comparisons.
Alternatively, use if(DEFINED VAR) to check if the variable is defined, then use ${VAR} in comparisons.
中文步骤
Use ${VAR} in the if() condition to expand the variable.Alternatively, use if(DEFINED VAR) to check if the variable is defined, then use ${VAR} in comparisons.
Dead Ends
Common approaches that don't work:
-
Adding quotes around the variable: if("${VAR}" STREQUAL "value")
10% fail
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.
-
Removing the STREQUAL keyword and using if(VAR value) thinking it's a different syntax
95% fail
CMake does not support if(VAR value) as a comparison; it will either error or give unexpected results.
-
Defining the variable with set(VAR "value") but still using bare VAR in if()
80% fail
Even if the variable is defined, bare VAR in if() checks if the literal string 'VAR' is defined, not the variable's value.