在 Makefile 中执行大于小于计算

发布于 2024-09-13 17:46:14 字数 218 浏览 2 评论 0原文

我正在尝试在 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 技术交流群。

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

发布评论

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

评论(4

弥枳 2024-09-20 17:46:15

试试这个

在本例中检查 VER 是否大于 4

ifeq ($(shell test $(VER) -gt 4; echo $?),0)
  IFLAGS += -I$(TOPDIR)/include
  LIBS += -L$(TOPDIR)/libs -lpcap
endif

Try this

In this example VER is checked for greater than 4

ifeq ($(shell test $(VER) -gt 4; echo $?),0)
  IFLAGS += -I$(TOPDIR)/include
  LIBS += -L$(TOPDIR)/libs -lpcap
endif
月隐月明月朦胧 2024-09-20 17:46:15

我首选的方法是:

value = 2.0
ifeq ($(shell expr $(value) \>= 1.5), 1)
   # execute a rule
else ifeq ($(shell expr $(value) \<= 0.5), 1)
   # execute a rule
endif

My preferred way to do this is:

value = 2.0
ifeq ($(shell expr $(value) \>= 1.5), 1)
   # execute a rule
else ifeq ($(shell expr $(value) \<= 0.5), 1)
   # execute a rule
endif
池木 2024-09-20 17:46:15

类似于这个问题,但基本上你可以在Makefile中使用shell命令。因此,以下内容是完全合法的:

foo:
    if [ ${value} -gt 2 ] ; then \
         #Do stuff;\
    fi

编辑一个小免责声明:IIRC,bash 不理解浮点运算。它可以将它们解释为字符串,但这可能会让事情变得有点奇怪。请务必考虑到这一点。

Similar to this question, but basically you can use shell commands inside a Makefile. So the following is perfectly legal:

foo:
    if [ ${value} -gt 2 ] ; then \
         #Do stuff;\
    fi

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.

吹梦到西洲 2024-09-20 17:46:15

正如其他答案中提到的,使用 shell 命令应该足以满足大多数用例:

if [ 1 -gt 0 ]; then \
    #do something \
fi

但是,如果您像我一样想要使用大于比较,以便那么通过make$(eval)命令设置一个make变量,然后你会发现试图使用其他答案的模型这样做:

if [ 1 -gt 0 ]; then \
    $(eval FOO := value) \
fi

引发错误:

if [ 1 -gt 0 ]; then  fi;
/bin/bash: -c: line 0: syntax error near unexpected token `fi'
/bin/bash: -c: line 0: `if [ 1 -gt 0 ]; then  fi;'
make: *** [clean] Error 2```

原因是 make$(eval) 表达式返回空字符串。 生成的 bash 代码格式错误。**

我想出了以下解决方案(在尝试了许多不同的方法之后!)。

$(if $(shell [ $(FOO) -ge 1 ] && echo "OK"), \
    $(eval BAR := true), \
    $(eval BAR := false))

执行算术比较后需要打印出一个字符串(echo "OK"部分),因为make$(if)基于空字符串逻辑的运算符:

$(if条件,then-part[,else-part])

if 函数在函数上下文中提供对条件扩展的支持(与 GNU make makefile 条件如 ifeq 不同(请参阅条件语法)。

第一个参数,条件,首先删除所有前面和后面的空格,然后扩展。如果它扩展为任何非空字符串,则条件被视为 true。如果它扩展为空字符串,则条件被视为错误。

来源:GNU Make 手册

希望这有帮助!

** 注意:最初,我认为通过添加一个不对表达式执行任何操作的 bash 命令可以轻松解决该问题,例如 true< /code>:

if [ $(VALUE) -ge 1 ]; then \
    true; $(eval BAR := GreaterOrEqualThan1) \
else \
    true; $(eval BAR := LowerThan1) \
fi

结果证明这是一个糟糕的主意。我仍然没有弄清楚为什么,但是 else 分支总是被执行,与比较的结果无关,

我认为我应该打开 。对于这个案例的一个问题。

Using shell commands, as mentioned in the other answers, should suffice for most use cases:

if [ 1 -gt 0 ]; then \
    #do something \
fi

However, if you, like me, want to use greater-than comparison in order to then set a make variable via make's $(eval) command, then you will find that attempting to do so using the other answer's model:

if [ 1 -gt 0 ]; then \
    $(eval FOO := value) \
fi

raises an error:

if [ 1 -gt 0 ]; then  fi;
/bin/bash: -c: line 0: syntax error near unexpected token `fi'
/bin/bash: -c: line 0: `if [ 1 -gt 0 ]; then  fi;'
make: *** [clean] Error 2```

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!).

$(if $(shell [ $(FOO) -ge 1 ] && echo "OK"), \
    $(eval BAR := true), \
    $(eval BAR := false))

It is necessary to print out a string after performing the arithmetic comparison (the echo "OK" part) because make's $(if) operator its based on empty-string logic:

$(if condition,then-part[,else-part])

The if function provides support for conditional expansion in a functional context (as opposed to the GNU make makefile conditionals such as ifeq (see Syntax of Conditionals).

The first argument, condition, first has all preceding and trailing whitespace stripped, then is expanded. If it expands to any non-empty string, then the condition is considered to be true. If it expands to an empty string, the condition is considered to be false.

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:

if [ $(VALUE) -ge 1 ]; then \
    true; $(eval BAR := GreaterOrEqualThan1) \
else \
    true; $(eval BAR := LowerThan1) \
fi

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.

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