Uboot最开始两句代码的一点解释 make最基础入门 触感make的灵活啦

本文深入解读嵌入式U-boot Makefile中的一行代码,揭示其背后的make灵活应用与编程语言特性。通过实例分析,探讨make命令的自动变量与替换功能,以及如何在实际开发中灵活运用这些技术。同时,文章还讨论了学习方法的选择与深度,强调灵活应用的重要性。
摘要由CSDN通过智能技术生成

嵌入式U-boot的Makefile中有这么一句
smdk2410_config : unconfig
@$(MKCONFIG) $(@:_config=) arm arm920t smdk2410 NULL s3c24x0

看上去很复杂的语法,但是当弄明白了怎么回事的时候才发现,其实只是将两个基本的语法用在一起了。

在此首先感受了make的灵活性,从一个侧面也看出了编程语言的灵活性,继续延展呢!manual中也说了,make不一定用于某个程序的自动编译,也可以用于一切需要自动随文件更新变化的场合,一切东西都是存在某些内在联系的!make灵活,世界也很灵活!尤其是中国人在某些方面很灵活!学会东西不难,学会灵活应用才难!

然后在查找答案的时候,我在想是读前人写好的中文解释,还是自己去读manual呢?这涉及到学习方法的问题!踏着前人的足迹,我们能更快的进步,但是反面我们能不能有前人的深度呢?完全看前人的好像是吃人家嚼过吐出来的东西,而完全自己看就好比为吃馒头自己从种麦子开始,这两者的结合当然是最好的但是这个度在哪?这恐怕像中国烹饪的火号一样难掌握了!鱼香肉丝多么伟大的一道菜,也许只有中国人能用胡萝卜抄出鱼肉的香味,中国人在度的研究上对我们来说是财富!

 

 

言规正传

 

smdk2410_config : unconfig

#这里目标是smdk2410_config也就是定义变量@的值 即$@=smdk2410_config
#(smdk2410_config:_config=)运算结果是smdk2410
#根据第一行变量定义可以知道$(@:_config=)=smdk2410

@$(MKCONFIG) $(@:_config=) arm arm920t smdk2410 NULL s3c24x0

第一个@表示不回显,即该命令并不直接出现在console中,而只是执行过程出现在console里
第二个
1.@并不是单独的,而是和$一起构成$@,表示目标(smdk2410_config),在这里的意思就是$@=smdk2410_config,可以把@看成变量名
2.var:a=b 的意思是把var中的a用b替换(这个朋友解释不太准确,不是var中应该是var最后,参看下面的manual),这样就很清楚 了,$(@:_config=)等价于(smdk2410_config:_config=),很显然这是要把smdk2410_config转变为 smdk2410

也就是第二个用了两个语法,这里把GUN make的manual贴出来,大家看看吧,还是不熟啊!

1.
10.5.3 Automatic Variables

Suppose you are writing a pattern rule to compile a `.c' file into a `.o' file: how do you write the `cc' command so that it operates on the right source file name? You cannot write the name in the command, because the name is different each time the implicit rule is applied.

What you do is use a special feature of make, the automatic variables. These variables have values computed afresh for each rule that is executed, based on the target and prerequisites of the rule. In this example, you would use `$@' for the object file name and `$<' for the source file name.

It's very important that you recognize the limited scope in which automatic variable values are available: they only have values within the command script. In particular, you cannot use them anywhere within the target list of a rule; they have no value there and will expand to the empty string. Also, they cannot be accessed directly within the prerequisite list of a rule. A common mistake is attempting to use $@ within the prerequisites list; this will not work. However, there is a special feature of GNU make, secondary expansion (see Secondary Expansion), which will allow automatic variable values to be used in prerequisite lists.

Here is a table of automatic variables:

$@
  The file name of the target of the rule. If the target is an archive member, then `$@' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), `$@' is the name of whichever target caused the rule's commands to be run. 
2.
6.3.1 Substitution References

A substitution reference substitutes the value of a variable with alterations that you specify. It has the form `$(var:a=b)' (or `${var:a=b}') and its meaning is to take the value of the variable var, replace every a at the end of a word with b in that value, and substitute the resulting string.

When we say “at the end of a word ”, we mean that a must appear either followed by whitespace or at the end of the value in order to be replaced; other occurrences of a in the value are unaltered. For example:

  foo := a.o b.o c.o
  bar := $(foo:.o=.c)

sets `bar' to `a.c b.c c.c'. See Setting Variables.

A substitution reference is actually an abbreviation for use of the patsubst expansion function (see Functions for String Substitution and Analysis). We provide substitution references as well as patsubst for compatibility with other implementations of make.

Another type of substitution reference lets you use the full power of the patsubst function. It has the same form `$(var:a=b)' described above, except that now a must contain a single `%' character. This case is equivalent to `$(patsubst a,b,$(var))'. See Functions for String Substitution and Analysis, for a description of the patsubst function.


For example:

   
  foo := a.o b.o c.o
  bar := $(foo:%.o=%.c)

sets `bar' to `a.c b.c c.c'.

 

 

顺便解释一下第二句整体的功能
@$(MKCONFIG) $(@:_config=) arm arm920t smdk2410 NULL s3c24x0
它等价于./mkconfig smdk2410 arm arm920t smdk2410 NULL s3c24x0
其中./mkconfig来自变量$(MKCONFIG)
smdk2410来自于上面解释
这一段arm arm920t smdk2410 NULL s3c24x0就是直接写出来的
MKCONFIG的定义来自于前面的代码
OBJTREE        := $(if $(BUILD_DIR),$(BUILD_DIR),$(CURDIR))
SRCTREE        := $(CURDIR)
TOPDIR        := $(SRCTREE)
LNDIR        := $(OBJTREE)
export    TOPDIR SRCTREE OBJTREE

MKCONFIG    := $(SRCTREE)/mkconfig
export MKCONFIG

ifneq ($(OBJTREE),$(SRCTREE))
REMOTE_BUILD     := 1
export REMOTE_BUILD

 

注 大括号内是引用互联网 感谢网友分享

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值