Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

CMake – Can not make decisions based on CMAKE_SYSTEM_NAME

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What am I doing wrong?

>Solution :

  1. To compare strings use STREQUAL. EQUAL is for numbers.
  2. 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading