Makefile和通配符 [英] Makefiles and wildcards

查看:154
本文介绍了Makefile和通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我当前的Makefile设置.有些文件名为public01.cpublic02.c等.我试图使用带有通配符的public*.o标签为每个文件创建目标文件.

Alright so this is my current setup for a makefile. There are files which are named public01.c, public02.c, etc. I'm trying to make object files for each of them using the public*.o label with a wildcard.

public*.o: public*.c hashtable.h
    $(CC) $(CFLAGS) -c public*.c

public*: public*.o
    $(CC) -o public* public*.o

但是,当我尝试运行makefile时,我得到了:

However, when I try and run the makefile, I get this:

make: *** No rule to make target `public*.c', needed by `public*.o'.  Stop.

我想它会将public*.c当作标签,而不是我想要的通配符.我阅读了有关$(wildcard pattern...)函数的内容并对其进行了试用,但是我并不真正理解它或无法使其正常工作...

I guess it's treating public*.c as a label and not a wildcard as I'd like. I read about $(wildcard pattern...) function and played around with it, but I didn't really understand it or got it to work...

推荐答案

简短答案:该语法无法按您希望的方式工作.在GNU make语法中执行所需操作的正确方法是使用模式规则:

Short answer: that syntax does not work the way you want it to. The correct way to do what you want in GNU make syntax is to use pattern rules:

public%.o: public%.c hashtable.h
        $(CC) $(CFLAGS) -c $<

public%: public%.o
        $(CC) -o $@ $<

长答案:此:

public*.o: public*.c hashtable.h

并不表示您想要的意思.假设在构建开始时有多个文件public01.c等,而没有文件public01.o等,则该语法与此等效:

does not mean what you want it to mean. Assuming you have several file public01.c, etc., and no files public01.o, etc., at the start of your build, that syntax is equivalent to this:

public*.o: public01.c public02.c public03.c ... hashtable.h

也就是说,如果public01.o等不存在,那么make将使用文字字符串public*.o作为文件名.如果确实存在某些.o文件,则该语法与此等效:

That is, if public01.o, etc., do not exist, then make will use the literal string public*.o as the filename. If some of the .o files do exist, then that syntax is equivalent to this:

public01.o public02.o ...: public01.c public02.c ... hashtable.h

看起来像您想要的吗?但这是对make的常见误解,因为实际上该行与此完全相同:

Seems like what you want right? But that's a common misunderstanding with make, because in fact that line is exactly the same as this:

public01.o: public01.c public02.c ... hashtable.h
public02.o: public01.c public02.c ... hashtable.h

也就是说-每个.o文件都依赖于每个.c文件!正确的方法是使用模式规则,如上所示.

That is -- every .o file has a dependency on every .c file! The correct way to do what you want is to use a pattern rule, as shown above.

这篇关于Makefile和通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆