Makefile 中的 @true 与 @false 命令

问题描述

在一些 Makefile 文件中,能够看到 @true 与 @false 这两个命令。我对这两个命令的功能不清楚,在本篇文章中探讨一下。

@ 与 @true 与 @false

@true 与 @false 均由两部分组成,第一部分是 @ 符号,第二部分是 true、false 命令。

@ 符号告诉 make 不要将这个命令输出到 stdout 中。
false 命令会返回一个非 0 值给 make 表示失败
true 命令会返回一个 0 值给 make 表示成功

false 与 @false 功能相同,只是 false 将会输出命令执行信息到 stdout 中。

make 在编译过程中,会逐行调用 Makefile 中的命令,如果某行返回了一个非 0 值(false),make 将会停止编译。在这种情况下 make 命令会认为编译目标文件构建失败,并且所有依赖这个目标文件的项目也将会失败,因此 make 命令将会失败,会返回一个非零的状态值

一个示例

查看如下 Makefile:

foo:
    @echo this will be printed '(foo)'
    @false
    @echo this will not be printed '(foo)'

bar: foo
    @echo this will not be printed '(bar)'

foo 中的第二个 echo 语句将不会出现,make 将在执行它上方的 @false 命令处失败并终止编译。

bar 也同样不会被编译因为它依赖的对象不被编译,因此你也不会看到 bar 中的 echo 命令的输出。

忽略命令执行失败的方法

如果你确认某一个脚本行将会失败,但是你想要忽略这个失败,你可以在该行的 Makefile 命令前添加一个 - 号。例如:

foo:
    @echo this will be printed '(foo)'
    -@false
    @echo this will also be printed '(foo)'

两个 echo 命令都会打印。false 命令将会运行,但是没有任何影响,执行后 false 命令的返回值因为 - 号的存在而被忽略。

dpdk 中的一个实例

dpdk 的编译脚本中生成 rte_config.h 的步骤中使用到了 @true 的功能。Makefile 中相关的代码摘录如下:

# clean installed files, and generate a new config header file
# if NODOTCONF variable is defined, don't try to rebuild .config
$(RTE_OUTPUT)/include/rte_config.h: $(RTE_OUTPUT)/.config
        $(Q)rm -rf $(RTE_OUTPUT)/include $(RTE_OUTPUT)/app \
                $(RTE_OUTPUT)/hostapp $(RTE_OUTPUT)/lib \
                $(RTE_OUTPUT)/hostlib $(RTE_OUTPUT)/kmod $(RTE_OUTPUT)/build
        $(Q)mkdir -p $(RTE_OUTPUT)/include
        $(Q)$(RTE_SDK)/scripts/gen-config-h.sh $(RTE_OUTPUT)/.config \
                > $(RTE_OUTPUT)/include/rte_config.h

# generate the rte_config.h
.PHONY: headerconfig
headerconfig: $(RTE_OUTPUT)/include/rte_config.h
        @true

这里 @true 语句的作用如下:

当 rte_config.h 文件存在且已经是最新的时候时,阻止构建 headerconfig 时 make 打印警告消息。

当 @true 去掉后,重新编译会看到如下打印:

make[3]: Nothing to be done for `headerconfig’.

参考链接:
What is false and true in makefile

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用VSCode调试包含多个Makefile的C++项目之前,需要先确保已安装以下工具: 1. C/C++插件 2. Make工具 3. GDB调试器 接下来,按照以下步骤进行操作: 1. 打开C++项目所在的文件夹,按下F5或者点击左侧调试栏的“启动调试”按钮。 2. 在弹出的“选择环境”窗口,选择“C++ (GDB/LLDB)”环境,然后点击“创建配置文件”。 3. 在打开的“launch.json”文件,添加以下配置: ``` { "name": "Debug Multi Makefile Project", "type": "cppdbg", "request": "launch", "program": "${workspaceRoot}/path/to/executable", "args": [], "stopAtEntry": true, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/path/to/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "Set breakpoint at main", "text": "break main", "ignoreFailures": true } ], "preLaunchTask": "build" } ``` 其,需要将“program”字段的值设置为项目生成的可执行文件的路径,将“miDebuggerPath”字段的值设置为GDB调试器的路径,将“setupCommands”字段的“text”值设置为需要执行的GDB命令,比如“break main”可以在程序开始执行时自动停在main函数处。 4. 在VSCode打开项目的“tasks.json”文件,在该文件配置需要执行的Makefile命令,比如: ``` { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "make -f makefile1 && make -f makefile2", "group": { "kind": "build", "isDefault": true } } ] } ``` 其,“command”字段的值为需要执行的Makefile命令,可以包括多个Makefile。 5. 按下F5或者点击左侧调试栏的“启动调试”按钮,等待程序运行到设置的断点处即可开始调试。 希望这些步骤能够对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值