且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GNU make manual 翻译(八十五)

更新时间:2022-08-19 14:27:54

继续翻译

复制代码
4.4.2 Pitfalls of Using Wildcards                        
---------------------------------  
Now here is an example of a naive way of using wildcard expansion, that
does not do what you would intend.  Suppose you would like to say that 
the executable file `foo' is made from all the object files in the
directory, and you write this:                        
                        
     objects = *.o                        
                        
     foo : $(objects)                        
             cc -o foo $(CFLAGS) $(objects)                        
                        
The value of `objects' is the actual string `*.o'.  Wildcard expansion happens in the rule for `foo', so that each _existing_ `.o' file becomes a prerequisite of `foo' and will be recompiled if necessary.  
                        
   But what if you delete all the `.o' files?  When a wildcard matches no files, it is left as it is, so then `foo' will depend on the oddly-named file `*.o'.  Since no such file is likely to exist, `make' will give you an error saying it cannot figure out how to make `*.o'. This is not what you want!                        
                        
   Actually it is possible to obtain the desired result with wildcard expansion, but you need more sophisticated techniques, including the `wildcard' function and string substitution.  *Note The Function `wildcard': Wildcard Function.  
                        
   Microsoft operating systems (MS-DOS and MS-Windows) use backslashes 
to separate directories in pathnames, like so:   
       c:\foo\bar\baz.c                        
                        
   This is equivalent to the Unix-style `c:/foo/bar/baz.c' (the `c:' part is the so-called drive letter).  When `make' runs on these  systems, it supports backslashes as well as the Unix-style forward slashes in pathnames.  However, this support does _not_ include the wildcard expansion, where backslash is a quote character.  Therefore, you _must_ use Unix-style slashes in these cases.                
复制代码
4.4.2 使用通配符的陷阱

--------------------------------- 

Now here is an example of a naive way of using wildcard expansion,

现在有一个以自然而然地方式使用通配符扩展的例子,但是给出的却不是你想要的结果。      
设想一下,你希望从目录下所有的.o 文件中生成 可执行程序 foo。你写了如下的代码:

objects = *.o

foo : $(objects)
cc -o foo $(CFLAGS) $(objects)
objects 的值是真正的字符串 *.o。通配符扩展发生在 foo 的规则李,因此 每一个已经存在的 .o 文件成为 foo 的前提条件,当有需要的时候,将会被重新编译。

但是当你删除所有的.o文件,会发生什么? 当通配符找不到匹配的文件,它将就此保留下来,所以 foo 就会依赖于一个拥有奇怪名字 *.o 的文件。由于没有此文件存在,make 将会告诉你一个错误信息表明无法生成 *.o , 这可不是你想要的!

事实上,想要获得预想的 通配符扩展的结果,也是可能的。但是你需要更加复杂的技术,包括 wildcard函数和 字符串切割。 *Note The Function 'wildcard': Wildcard Function

Microsoft 操作系统(MS-DOS 和 MS-Windows) 使用反斜线来分隔目录路径名,类似于:

c:\foo\bar\baz.c

这等价于 Unix 下的 c:/foo/bar/baz.c (c:部分是驱动器字母)。当 make 在这样的系统上运行时,它既支持反斜线也支持Unix形式的斜线。 但是这种支持并不包括 通配符扩展,此时反斜线是一个 需要放在引号中的字符。因此,遇到通配符的时候,你必须用Unix 格式。

后文待续


本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/09/19/2693366.html,如需转载请自行联系原作者