为什么CMake语法在任何地方都有冗余括号? [英] Why does CMake syntax have redundant parentheses everywhere?

查看:216
本文介绍了为什么CMake语法在任何地方都有冗余括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMake的如果如下所示:

CMake's ifs go like this:

if (condition)
    ...
else if (...)
    ...
else (...)
    ...
endif (...)

使用 else if(...) (...)测试单独的条件。

为什么 else(...)而不只是 else ?为什么 endif(...)而不是 endif

With else if (...) the (...) tests for a separate condition.
Why else (...) and not just else? Why endif (...) and not endif?

Cmake的函数如下:

Cmake's functions go like this:

function(funcname ...)
    ...
endfunction(funcname ...)

为什么 endfunction(funcname ...)而不是简单地 endfunction

我可以省略它们出现的冗余括号的内容,例如: endif()。这个结构的目的是什么?

I can omit the contents of the redundant parenthesis where they appear, like so: endif (). What's the purpose of this construct?

推荐答案

我相信最初的意图是,通过在每个子句重复(例如 else 语句)初始表达式(例如, if 语句)

I believe the initial intention was that, by repeating at each clause (for example, else statement) the initial expression (for example, the one at the if statement) it would make more clear which statement was actually closed, and the parser could verify and warn nothing wrong was going on.

但是,结果是你会有如下的表达式:

However, it turned out that you would have expression like:

if (VARIABLE matches "something")
[...] #This is executed when above condition is true
else (VARIABLE matches "something") #Looks very much like an elseif...
[...] #This is executed when above condition is false!
endif (VARIABLE matches "something")

我在谈论我的每一天的经验,例如。我写了一些东西,有人来问我这是什么?

Which turned out to be confusing. I am speaking about my every day experience, e.g. I write something and somebody else come to ask me "What does this does?"

现在CMake也允许放空括号,上面可以改写为:

So, now CMake allows also to put empty parenthesis, the above can be rewritten as:

if (VARIABLE matches "something")
[...] #This is executed when above condition is true
else ()
[...] #This is executed when above condition is false
endif ()

这可以被视为更清楚。

Which can be considered clearer. The grammar above can still be used.

为了完全回答你的问题,圆括号也保留空参数,因为从概念上来说 else endif 的宏,如果,因此它们是用这个语法调用的,小位(但不完全正确)作为函数。



基本上相同的解释在CMake常见问题解答:不是ELSE(Expression)中的Expression混淆?

To completely answer to your question, the parenthesis stay also with empty arguments because conceptually else and endif in CMake are macros like if, and therefore they are invoked with this grammar, a little bit (but not at all exactly) as functions.


Basically the same explanation is available in CMake FAQs: Isn't the "Expression" in the "ELSE (Expression)" confusing?.

要添加一点历史,在CMake 2.4中重复的表达式是强制性的,仍然在 CMake 2.6 ,至少根据文档。文档在 else 上含糊不清,但在 endif b

To add a little bit of history, in CMake 2.4 repeating the expression was obligatory, and still in CMake 2.6, at least according to the documentation. The documentation was ambiguous on else, but adamant on endif:


请注意,必须为if和endif指定相同的表达式。

Note that the same expression must be given to if, and endif.

删除此约束已经在CMake 2.4.3(2006年版)中引入,可以通过写入来取消激活它:

The first try to remove this constraint was introduced already with CMake 2.4.3 (year 2006), where it was possible to deactivate it by writing:

set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

此约束对于 CMake 2.8


请注意,else和endif子句中的表达式是可选的。

Note that the expression in the else and endif clause is optional.

这篇关于为什么CMake语法在任何地方都有冗余括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆