currently I am working on a project using CMake.
I want to do stuff based on the value of CMAKE_SYSTEM_NAME.
It seems that the CMake if statement does not work as I expected.
message(STATUS "CMAKE_SYSTEM_NAME = >${CMAKE_SYSTEM_NAME}<")
if(${CMAKE_SYSTEM_NAME} EQUAL "Windows")
message(STATUS "Why am I not called?")
else()
message(STATUS "I should not be here!\nCMAKE_SYSTEM_NAME = >${CMAKE_SYSTEM_NAME}<.")
endif()
# Output:
# > [CMake] -- CMAKE_SYSTEM_NAME = >Windows<
# > [CMake] -- I should not be here!
# > [CMake] CMAKE_SYSTEM_NAME = >Windows<.
The message before the if shows that CMAKE_SYSTEM_NAME is set to "Windows" but still the script lands on the else.
What am I doing wrong?
>Solution :
- To compare strings use STREQUAL. EQUAL is for numbers.
- STREQUAL expands both sides as variables. Do not
${...}.
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
See https://cmake.org/cmake/help/latest/command/if.html#variable-expansion .
Why am I not called?
EQUAL just returns false if there are invalid numbers.