免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5845 | 回复: 7
打印 上一主题 下一主题

[内核入门] 请教makefile语法问题 [复制链接]

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015元宵节徽章
日期:2015-03-06 15:52:30
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-05-04 09:36 |只看该作者 |正序浏览


内核的Makefile有一行是这样的,

mrproper: rm-dirs := $(wildcard $(MRPROPER_DIRS))

我想问这是什么意思,怎么写依赖的时候还能够赋值?

我的理解是给 rm-dirs赋值,然后mrproper依赖于 $(rm-dirs)对吗?

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
8 [报告]
发表于 2014-05-05 08:45 |只看该作者
回复 7# qianguozheng
不用你帮顶了,其实楼主自己已经得到答案了。
就是Target-specific Variable Values。下面把全文都拷出来吧。方便其它人阅读。

6.11 Target-specific Variable Values

Variable values in make are usually global; that is, they are the same regardless of where they are evaluated (unless they're reset, of course). One exception to that is automatic variables (see Automatic Variables).

The other exception is target-specific variable values. This feature allows you to define different values for the same variable, based on the target that make is currently building. As with automatic variables, these values are only available within the context of a target's recipe (and in other target-specific assignments).

Set a target-specific variable value like this:

     target ... : variable-assignment
Target-specific variable assignments can be prefixed with any or all of the special keywords export, override, or private; these apply their normal behavior to this instance of the variable only.

Multiple target values create a target-specific variable value for each member of the target list individually.

The variable-assignment can be any valid form of assignment; recursive (‘=’), simple (‘:=’ or ‘::=’), appending (‘+=’), or conditional (‘?=’). All variables that appear within the variable-assignment are evaluated within the context of the target: thus, any previously-defined target-specific variable values will be in effect. Note that this variable is actually distinct from any “global” value: the two variables do not have to have the same flavor (recursive vs. simple).

Target-specific variables have the same priority as any other makefile variable. Variables provided on the command line (and in the environment if the ‘-e’ option is in force) will take precedence. Specifying the override directive will allow the target-specific variable value to be preferred.

There is one more special feature of target-specific variables: when you define a target-specific variable that variable value is also in effect for all prerequisites of this target, and all their prerequisites, etc. (unless those prerequisites override that variable with their own target-specific variable value). So, for example, a statement like this:

     prog : CFLAGS = -g
     prog : prog.o foo.o bar.o
will set CFLAGS to ‘-g’ in the recipe for prog, but it will also set CFLAGS to ‘-g’ in the recipes that create prog.o, foo.o, and bar.o, and any recipes which create their prerequisites.

Be aware that a given prerequisite will only be built once per invocation of make, at most. If the same file is a prerequisite of multiple targets, and each of those targets has a different value for the same target-specific variable, then the first target to be built will cause that prerequisite to be built and the prerequisite will inherit the target-specific value from the first target. It will ignore the target-specific values from any other targets.



   

论坛徽章:
7
IT运维版块每日发帖之星
日期:2016-05-27 06:20:00IT运维版块每日发帖之星
日期:2016-06-09 06:20:00操作系统版块每日发帖之星
日期:2016-06-12 06:20:00程序设计版块每日发帖之星
日期:2016-06-12 06:20:00操作系统版块每日发帖之星
日期:2016-06-13 06:20:00IT运维版块每日发帖之星
日期:2016-06-17 06:20:002015-2016NBA季后赛纪念章
日期:2016-06-28 17:42:27
7 [报告]
发表于 2014-05-04 22:58 |只看该作者
只能帮顶了,兄弟,学习

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015元宵节徽章
日期:2015-03-06 15:52:30
6 [报告]
发表于 2014-05-04 15:39 |只看该作者
This feature allows you to define different values for the same variable, based on the target that make is currently building.


looks a bit like namespace feature

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015元宵节徽章
日期:2015-03-06 15:52:30
5 [报告]
发表于 2014-05-04 15:36 |只看该作者
the answer is in the Section of Target-specific Variable Values in GNU manual.

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015元宵节徽章
日期:2015-03-06 15:52:30
4 [报告]
发表于 2014-05-04 10:28 |只看该作者

我又写了个测试的程序和两个makefile
第一个makefile可以正常编译hello.c为可执行文件clean
第二个出错,$<为空

  1. /* hello.c */
  2. #include <stdio.h>

  3. int main(void) {

  4.    printf("hello world\n");
  5.    return 0;
  6. }
复制代码
第一个makefile

  1. clean: hello.c

  2. clean:
  3.         $(CC) -o $@ $<

复制代码
第二个makefile

  1. clean: var := hello.c

  2. clean:
  3.         $(CC) -o $@ $<

复制代码

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015元宵节徽章
日期:2015-03-06 15:52:30
3 [报告]
发表于 2014-05-04 09:54 |只看该作者

我写了两个测试的makefile

第一个


  1. clean: var := hello
  2.         echo $(var)

复制代码
第二个


  1. clean: var := hello

  2. clean:
  3.         echo $(var)
复制代码
第一个出错,Makefile:2: *** recipe commences before first target.  Stop.
第二个能够输出 hello

论坛徽章:
0
2 [报告]
发表于 2014-05-04 09:49 |只看该作者
  不了解, 我也没见过依赖的时候还能赋值
  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP