CMake's STREQUAL not working
According to the CMak开发者_StackOverflowe documentation, the STREQUAL
comparison is allowed to take either a VARIABLE or a STRING as either parameter. So, in this example below, the message does NOT print, which is broken:
set( FUBARTEST "OK" )
if( FUBARTEST STREQUAL "OK" )
message( "It Worked" )
endif()
Any reason why this isn't working as documented?
The issue was my cache. I deleted my cache and reconfigured and now the code works.
I didn't test your example at first, but when I did, I see your code works fine on cmake 2.8.0, and the other combinations advertised in the docs do too:
set( FUBARTEST "OK" )
if( FUBARTEST STREQUAL "OK" )
message( "FUBARTEST Worked" )
else()
message( "FUBARTEST FAILED" )
endif()
set( FOO "OK" )
if( ${FOO} STREQUAL "OK" )
message("string STREQUAL string works" )
else ()
message("string STREQUAL string FAILED" )
endif()
set( FOO "OK" )
set( BAR "OK" )
if( FOO STREQUAL BAR )
message("variable STREQUAL variable works" )
else ()
message("variable STREQUAL variable FAILED" )
endif()
set( FOO "OK" )
if( FOO STREQUAL "OK" )
message("variable STREQUAL string works" )
else ()
message("variable STREQUAL string FAILED" )
endif()
gives output:
FUBARTEST Worked
string STREQUAL string works
variable STREQUAL variable works
variable STREQUAL string works
The same thing happens when using '
instead of "
for the string comparison
This won't work:
if( FUBARTEST STREQUAL 'OK' )
message( "It Worked" )
endif()
This works (except if there is a cache issue like mentioned above):
if( FUBARTEST STREQUAL "OK" )
message( "It Worked" )
endif()
I had such problem on Windows, when on Linux all worked fine.
Turns out issue was, that on Linux logical operations can be written before cmake_minimum_required and project, when on Windows they must be placed after.
Working example:
cmake_minimum_required( VERSION 3.16 )
project( "strequal_example" VERSION 1.0 LANGUAGES C CXX )
if ( ${CMAKE_BUILD_TYPE} STREQUAL "Debug" )
message( STATUS "DEBUG mode enabled" )
endif()
And this example works only on Linux with CMake 3.16, but not on Windows:
if ( ${CMAKE_BUILD_TYPE} STREQUAL "Debug" )
message( STATUS "DEBUG mode enabled" )
endif()
cmake_minimum_required( VERSION 3.16 )
project( "strequal_example" VERSION 1.0 LANGUAGES C CXX )
精彩评论