IFDEF和MAKE:条件中的语法无效

发布于 2025-02-10 21:30:50 字数 664 浏览 2 评论 0原文

我是新手。我只是想检查给出的成品的输入是否正确,如果用户不给出任何值,我将其设置为“ 0”。遇到的错误如下:

Makefile:11 *** invalid syntax in conditional. stop.

以下是我编写的一些代码:

ifndef farm_batch
 farm_batch = 0
endif

ifdef farm_batch \             #<--------- line 11 ---------
 ifneq ($(farm_batch),0) \
  echo "please input the correct value" \
 endif \
endif \

target:
    #do something here using $(farm_batch)

注意:

  • 删除反弹时,我遇到了其他错误,这些错误是:

      makefile:13:***缺少SEPERATOR。停止。
    
     makefile:255:***缺少'endif'。停止。
     
  • 示例:

     使目标farm_batch = 9
     

谢谢您的宝贵时间。

I am new in make. I was just trying to check whether the input given to the make is correct or not and if the user does not give any value, I'll just set it to '0'. The error encountered are as follows:

Makefile:11 *** invalid syntax in conditional. stop.

Below are some of the code that I've written:

ifndef farm_batch
 farm_batch = 0
endif

ifdef farm_batch \             #<--------- line 11 ---------
 ifneq ($(farm_batch),0) \
  echo "please input the correct value" \
 endif \
endif \

target:
    #do something here using $(farm_batch)

Note:

  • When I remove the backlashes, I encountered additional errors which are:

     Makefile:13: *** missing seperator. stop.
    
     Makefile:255: *** missing 'endif'. stop.
    
  • Example:

     make target farm_batch=9
    

Thank you for your time.

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

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

发布评论

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

评论(1

一直在等你来 2025-02-17 21:30:50

因此,您要使变量farm_batch具有默认值(0),并且还检查它确实具有此值,否则打印一条消息。

首先,命令行变量分配覆盖了makefile分配。因此,对于默认值,只需将以下行添加到您的makefile:

farm_batch := 0

如果仅键入make,则farm_batch的值将为0。如果您键入make farm_batch = 1它将是1

接下来,请勿混合制作语法和shell语法(echo是shell命令)。 Shell语法仅用于规则的配方(几乎),即以标签开始的规则中的所有行。在其他任何地方,都使用制作语法。

使用GNU MAKE,如果您想打印消息并退出farm_batch不等于0 :(

$ cat Makefile
farm_batch := 0

ifneq ($(farm_batch),0)
$(error please input the correct value)
endif

target:
<TAB>@echo "$(farm_batch): OK"

$ make
0: OK

$ make farm_batch=1
Makefile:4: *** please input the correct value.  Stop.

替换&lt; tab&lt; tab&gt;选项卡,<代码> info警告如果您需要使用错误>错误在此处停止和退出)。

使用另一个make不提供info警告错误>错误函数,您将不得不打印食谱中的消息:(

$ cat Makefile
farm_batch := 0

target:
ifneq ($(farm_batch),0)
<TAB>@echo "please input the correct value"
<TAB>exit 1
else
<TAB>@echo "$(farm_batch): OK"
endif

$ make
0: OK

$ make farm_batch=1
please input the correct value
exit 1
make: *** [Makefile:6: target] Error 1

再次,用选项卡替换&lt; tab&gt;)。请注意,您还可以用外壳在食谱内进行测试,而不是使用有条件的条件:

$ cat Makefile
farm_batch := 0

target:
<TAB>@if [ "$(farm_batch)" -ne 0 ]; then \
      echo "please input the correct value"; \
      exit 1; \
    else \
      echo '$(farm_batch): OK'; \
    fi

$ make
0: OK

$ make farm_batch=1
please input the correct value
make: *** [Makefile:4: target] Error 1

So, you want make variable farm_batch to have a default value (0) and also to check that it indeed has this value, else print a message.

First of all, command-line variable assignments override Makefile assignements. So, for the default value simply add the following line to your Makefile:

farm_batch := 0

If you type just make, the value of farm_batch will be 0. If you type make farm_batch=1 it will be 1.

Next, don't mix make syntax and shell syntax (echo is a shell command). Shell syntax is for rules' recipes only (almost), that is, all lines in a rule that start with a tab. Everywhere else, use make syntax.

With GNU make, if you want to print a message and exit when farm_batch is not equal to 0:

$ cat Makefile
farm_batch := 0

ifneq ($(farm_batch),0)
$(error please input the correct value)
endif

target:
<TAB>@echo "$(farm_batch): OK"

$ make
0: OK

$ make farm_batch=1
Makefile:4: *** please input the correct value.  Stop.

(replace <TAB> with a tab, and replace error by info or warning if you want a different severity level; with error make stops here and exits).

With another make that does not provide the info, warning and error functions you will have to put the printing of the message inside a recipe:

$ cat Makefile
farm_batch := 0

target:
ifneq ($(farm_batch),0)
<TAB>@echo "please input the correct value"
<TAB>exit 1
else
<TAB>@echo "$(farm_batch): OK"
endif

$ make
0: OK

$ make farm_batch=1
please input the correct value
exit 1
make: *** [Makefile:6: target] Error 1

(again, replace <TAB> by tabs). Note that you can also do the test with the shell, inside the recipe, instead of using make conditionals:

$ cat Makefile
farm_batch := 0

target:
<TAB>@if [ "$(farm_batch)" -ne 0 ]; then \
      echo "please input the correct value"; \
      exit 1; \
    else \
      echo '$(farm_batch): OK'; \
    fi

$ make
0: OK

$ make farm_batch=1
please input the correct value
make: *** [Makefile:4: target] Error 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文