Makefile 和通配符
好吧,这是我当前的 makefile 设置。有些文件名为 public01.c
、public02.c
等。我尝试使用 public*.c 为每个文件创建目标文件。 o 带通配符的
标签。
public*.o: public*.c hashtable.h
$(CC) $(CFLAGS) -c public*.c
public*: public*.o
$(CC) -o public* public*.o
然而,当我尝试运行 makefile 时,我得到了这样的信息:
make: *** No rule to make target `public*.c', needed by `public*.o'. Stop.
我猜它将 public*.c
视为标签,而不是我想要的通配符。我阅读了有关 $(wildcard pattern...)
函数并使用了它,但我并没有真正理解它或让它工作......
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
However, when I try and run the makefile, I get this:
make: *** No rule to make target `public*.c', needed by `public*.o'. Stop.
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短回答:该语法无法按照您希望的方式工作。在 GNU make 语法中执行您想要的操作的正确方法是使用模式规则:
长答案:This:
并不意味着您想要它的含义。假设您在构建开始时有多个文件
public01.c
等,并且没有文件public01.o
等,则该语法等效于:也就是说,如果
public01.o
等不存在,那么 make 将使用文字字符串public*.o
作为文件名。如果某些.o
文件确实存在,那么该语法相当于:看起来像您想要的,对吧?但这是 make 的一个常见误解,因为事实上该行与此完全相同:
即 - 每个
.o
文件都依赖于每个.c
文件!执行您想要的操作的正确方法是使用模式规则,如上所示。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:
Long answer: This:
does not mean what you want it to mean. Assuming you have several file
public01.c
, etc., and no filespublic01.o
, etc., at the start of your build, that syntax is equivalent to this:That is, if
public01.o
, etc., do not exist, then make will use the literal stringpublic*.o
as the filename. If some of the.o
files do exist, then that syntax is equivalent to this: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:
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.发生该错误通常是因为 gcc 认为该文件不存在。确保它存在并确保您位于它存在的目录中,然后它应该可以工作。
另外,您没有为公众*提供 $(CFLAGS) 的原因是什么?到底为什么它是公开的*?我相信只要有“公共”就足够了。
That error usually occurs because the gcc thinks the file doesn't exist. Make sure it exists and make sure you're in the directory where it exists, and then it should work.
Also, any reason you don't have $(CFLAGS) for the public*? Why exactly is it public*? Simply having "public" would suffice I believe.