CMake ENDIF 和 ELSE 参数的用途
Purpose of CMake ENDIF and ELSE arguments
在 CMake 中,ELSE
和 ENDIF
控制流函数将表达式作为参数。根据文档,这些是 optional。那这些的目的是什么?是为了让原来的IF
表达式更清晰方便维护,还是提供了一些功能?
并不是说else和endif是可选的。 () 内的表达式是可选的。来自文档:
Note that the expression in the else and endif clause is optional.
以前版本的 cmake 要求您在 else 和 endif 中重复条件:
if(FOO)
...
else(FOO)
...
endif(FOO)
可选参数可以更容易地找到匹配的 if/else/endif 部分,从而提高可读性。
我个人不使用参数,因为我发现 else 语句 else(condition)
真的像
中那样令人困惑
if(condition)
// do something
else(condition)
// do something else
endif(condition)
我经常把else(condition)
误读成elseif(condition)
。
正如您所说,这些表达式是可选的,当您嵌套 if()
语句时它们很有用 - 当 endif()
中的 expr
与 [=11] 不匹配时,cmake 会警告您=] 在最近的 if()
.
else()
也是如此。
简单 - 这可以保护您免受 if()
else()
endif()
嵌套链中的错误。
else() 和 endif() 的参数在 2.6.0 版本之前是必需的。来自 CMake FAQ:
As of CMake 2.6.0 the ELSE() and ENDIF() constructs can be empty. The same is true for closing constructs on ENDMACRO(), ENDFUNCTION(), and ENDFOREACH(). If you require 2.4.x compatibility, CMake 2.4.3 or greater recognizes the CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS option (which is superfluous in 2.6.0)
除了提高可读性外,它们似乎没有任何作用。
另见 。
在 CMake 中,ELSE
和 ENDIF
控制流函数将表达式作为参数。根据文档,这些是 optional。那这些的目的是什么?是为了让原来的IF
表达式更清晰方便维护,还是提供了一些功能?
并不是说else和endif是可选的。 () 内的表达式是可选的。来自文档:
Note that the expression in the else and endif clause is optional.
以前版本的 cmake 要求您在 else 和 endif 中重复条件:
if(FOO)
...
else(FOO)
...
endif(FOO)
可选参数可以更容易地找到匹配的 if/else/endif 部分,从而提高可读性。
我个人不使用参数,因为我发现 else 语句 else(condition)
真的像
if(condition)
// do something
else(condition)
// do something else
endif(condition)
我经常把else(condition)
误读成elseif(condition)
。
正如您所说,这些表达式是可选的,当您嵌套 if()
语句时它们很有用 - 当 endif()
中的 expr
与 [=11] 不匹配时,cmake 会警告您=] 在最近的 if()
.
else()
也是如此。
简单 - 这可以保护您免受 if()
else()
endif()
嵌套链中的错误。
else() 和 endif() 的参数在 2.6.0 版本之前是必需的。来自 CMake FAQ:
As of CMake 2.6.0 the ELSE() and ENDIF() constructs can be empty. The same is true for closing constructs on ENDMACRO(), ENDFUNCTION(), and ENDFOREACH(). If you require 2.4.x compatibility, CMake 2.4.3 or greater recognizes the CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS option (which is superfluous in 2.6.0)
除了提高可读性外,它们似乎没有任何作用。
另见