在 Makefile 中执行大于小于计算
我正在尝试在 Makefile 中执行此操作:
value = 2.0
if ${greaterthan ${value}, 1.50}
-> execute a rule
elseif ${lessthan ${value}, 0.50}
-> execute a rule
endif
这似乎是一件很常见的事情。这样做的最佳方法是什么?
I'm trying to do this in a Makefile:
value = 2.0
if ${greaterthan ${value}, 1.50}
-> execute a rule
elseif ${lessthan ${value}, 0.50}
-> execute a rule
endif
It seems like quite a common thing to want to do. What's the best way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个
在本例中检查 VER 是否大于 4
Try this
In this example VER is checked for greater than 4
我首选的方法是:
My preferred way to do this is:
类似于这个问题,但基本上你可以在Makefile中使用shell命令。因此,以下内容是完全合法的:
编辑一个小免责声明:IIRC,
bash
不理解浮点运算。它可以将它们解释为字符串,但这可能会让事情变得有点奇怪。请务必考虑到这一点。Similar to this question, but basically you can use shell commands inside a Makefile. So the following is perfectly legal:
Edit for a small disclaimer: IIRC,
bash
doesn't understand floating point arithmetic. It can interpret them as strings, but it might make things a little weird. Make sure you take this into consideration.正如其他答案中提到的,使用 shell 命令应该足以满足大多数用例:
但是,如果您像我一样想要使用大于比较,以便那么通过
make
的$(eval)
命令设置一个make
变量,然后你会发现试图使用其他答案的模型这样做:引发错误:
原因是
make
的$(eval)
表达式返回空字符串。 生成的 bash 代码格式错误。**我想出了以下解决方案(在尝试了许多不同的方法之后!)。
执行算术比较后需要打印出一个字符串(
echo "OK"
部分),因为make
的$(if)
基于空字符串逻辑的运算符:来源:GNU Make 手册
希望这有帮助!
** 注意:最初,我认为通过添加一个不对表达式执行任何操作的 bash 命令可以轻松解决该问题,例如
true< /code>:
结果证明这是一个糟糕的主意。我仍然没有弄清楚为什么,但是
else
分支总是被执行,与比较的结果无关,我认为我应该打开 。对于这个案例的一个问题。
Using shell commands, as mentioned in the other answers, should suffice for most use cases:
However, if you, like me, want to use greater-than comparison in order to then set a
make
variable viamake
's$(eval)
command, then you will find that attempting to do so using the other answer's model:raises an error:
The reason is that
make
's$(eval)
expression returns the empty string. The resulting bash code is then malformed.**I came up with the following solution (after trying many different approaches!).
It is necessary to print out a string after performing the arithmetic comparison (the
echo "OK"
part) becausemake
's$(if)
operator its based on empty-string logic:Source: GNU Make Manual
Hope this helps!
** Note: Initially, I thought that that issue could be easily fixed by adding a bash command that doesn't do anything to the expression, such as
true
:That turned out to be a terrible idea. I still haven't figured out why, but the
else
branch is always executed, independently of the result of the comparison.I think I should open a question for this case.