CMake 的 STREQUAL 不起作用

发布于 2024-12-08 06:33:42 字数 236 浏览 0 评论 0原文

根据 CMake 文档,STREQUAL 比较允许采用 VARIABLE 或 STRING 作为任一参数。因此,在下面的示例中,该消息不会打印,这是损坏的:

set( FUBARTEST "OK" )
if( FUBARTEST STREQUAL "OK" )
    message( "It Worked" )
endif()

有什么原因导致这不能按记录工作?

According to the CMake 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光倒影 2024-12-15 06:33:43

问题是我的缓存。我删除了缓存并重新配置,现在代码可以工作了。

The issue was my cache. I deleted my cache and reconfigured and now the code works.

染火枫林 2024-12-15 06:33:43

我一开始没有测试你的示例,但是当我测试时,我发现你的代码在 cmake 2.8.0 上运行良好,并且文档中宣传的其他组合也这样做:

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()

给出输出:

FUBARTEST Worked
string STREQUAL string works
variable STREQUAL variable works
variable STREQUAL string 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
抚笙 2024-12-15 06:33:43

当使用 ' 而不是 " 进行字符串比较时,也会发生同样的情况

这不起作用:

if( FUBARTEST STREQUAL 'OK' )
    message( "It Worked" )
endif()

这有效(除非存在如上所述的缓存问题):

if( FUBARTEST STREQUAL "OK" )
    message( "It Worked" )
endif()

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()
挽你眉间 2024-12-15 06:33:43

我在 Windows 上遇到了这样的问题,在 Linux 上一切正常。

结果问题是,在 Linux 上,逻辑操作可以写在 cmake_minimum_required 和项目之前,而在 Windows 上,它们必须放在后面。

工作示例

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()

此示例仅适用于使用 CMake 3.16 的 Linux,但不适用于 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 )

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文