captivated 发表于 2011-05-23 04:36

回复 10# shimmey


    手册我查了... 就是查了仍然不解, 所以才问... 没有查GNUmake原英文文档, 我想我也应该查一下(一时间不知道哪里有这个,不过应该能google到,呵呵)... 但是估计英文文档的说明可能令我更不明...

    当然了... 如果您能帮助查这个问题并给予回复和例示, 那真是非常感谢... 这个问题着实困扰了我许久, 现在仍然...

captivated 发表于 2011-05-23 11:17

查了GNUmake英文文档, 其意思跟中文文档一样的, 给的示例也一样.

一下是GNUmake 原英文文档对eval函数的解释:

8.8 The eval Function

The eval function is very special: it allows you to define new makefile constructs that are not constant; which are the result of evaluating other variables and functions. The argument to the eval function is expanded, then the results of that expansion are parsed as makefile syntax. The expanded results can define new make variables, targets, implicit or explicit rules, etc.

The result of the eval function is always the empty string; thus, it can be placed virtually anywhere in a makefile without causing syntax errors.

It's important to realize that the eval argument is expanded twice; first by the eval function, then the results of that expansion are expanded again when they are parsed as makefile syntax. This means you may need to provide extra levels of escaping for "$" characters when using eval. The value function (see section 8.7 The value Function) can sometimes be useful in these situations, to circumvent unwanted expansions.

Here is an example of how eval can be used; this example combines a number of concepts and other functions. Although it might seem overly complex to use eval in this example, rather than just writing out the rules, consider two things: first, the template definition (in PROGRAM_template) could need to be much more complex than it is here; and second, you might put the complex, "generic" part of this example into another makefile, then include it in all the individual makefiles. Now your individual makefiles are quite straightforward.

...... 一句话, 仍然不明白.

huxk 发表于 2011-05-23 11:41

这两句应该是关键
The argument to the eval function is expanded, then the results of that expansion are parsed as makefile syntax.

It's important to realize that the eval argument is expanded twice; first by the eval function, then the results of that expansion are expanded again when they are parsed as makefile syntax. This means you may need to provide extra levels of escaping for "$" characters when using eval.

如果你期望的结果是$(aa)那么 这样的表达式$($($(xx))) 两次展开后 剩下的结果才是

兄弟 你太好玩了

captivated 发表于 2011-05-23 14:38

回复 13# huxk


    呵呵, 多谢捧场啦. 不过eval的二次展开不是你说的那个意思.

    $($($(xx)))这种, 只是变量递归展开而已, 就是说变量会在使用时递归展开.

    比如你写一个Makefile:xx = asdfg

.PHONY: all
all:
      @echo $($($(xx)))~~~上面的代码执行,结果会是显示"~~~". 因为$($($(xx)))是一个空值: $(xx) = asdfg, $($(xx)) = $(asdfg) = null, $($($(xx))) = $(null) = null.

   eval函数不是那个意思...

huxk 发表于 2011-05-23 15:46

啊 这个难道不是说 让 eval 建立 形如 server : $(server_OBJS) –l$(server_LIBS) 这样的依赖规则的么

另外你的那个 示例makefile和官方文档似乎不一样吧 $(1): $($(1)_OBJ) $($(1)_LIBS:%=-l%) 少了个 $ 吧

captivated 发表于 2011-05-23 17:57

回复 15# huxk


    哦, 多谢提醒. 少的那个$补上了. 被该死的CU代码引用吞掉了一个... 奇怪...

    回头仔细看了下... 嗯, 其实并不困难. 我基本了解了. TKS...

thelordsaves 发表于 2012-04-11 00:27

多谢楼主,我自己也弄明白了,我发现其实这些东西都不难,多看几遍,多体会几次,上机练一练,就能明白

三月廿七 发表于 2012-04-11 07:51

回复 11# captivated


    原来大叔也有这么一段

cjdao 发表于 2012-04-11 08:15

解释不了,顶一下:mrgreen:

liwangli1983 发表于 2012-04-11 08:45

个人感觉eval挺像c++的模板的,make处理到eval时先把eval按规则展开,然后再把展开内容处理一遍。
页: 1 [2] 3
查看完整版本: 我一直没弄明白makefile里面的eval函数,哪位大侠详细讲讲?