问题描述
SOURCES = server.c
TARGET = Server
CC = gcc
all: $(SOURCES) $(TARGET)
$(CC) $(SOURCES) -o $(TARGET)
clean:
rm -rf $(TARGET)
上面是我的Web服务器的Makefile.虽然server.c文件位于目录中,但这会产生休闲错误
Above is the Makefile of my web server. Though server.c file is in the directory this gives the fallowing error
make: *** No rule to make target `Server', needed by `all'. Stop.
我犯了什么错误以及如何解决它.
What is the mistake I've made and how to solve it.
推荐答案
我认为您的makefile在您的机器和帖子之间的某个地方出现了乱码,但是有一个简单的修复方法我认为可以解决:
I think your makefile got garbled somewhere between your machine and the post, but there is a simple fix that I think will work:
all: $(SOURCES)
这(可能)将解决问题并消除错误-如果仅此而已,您可以停止阅读.但是此makefile仍然有问题,因此我们可以进行更多改进.
That will (probably) solve the problem and make the error go away-- if that's all you want then you can stop reading. But there are still things wrong with this makefile, so we can make some more improvements.
首先,进行一些调整,使其与我认为您的makefile真正说的相符:
First, a little adjustment to make it match what I think your makefile really says:
SOURCES = server.c
TARGET = Server
CC = gcc
all: $(SOURCES) $(TARGET)
$(CC) $(SOURCES) -o $(TARGET)
clean:
rm -rf $(TARGET)
前三行和clean
规则都可以,我们将忽略它们.现在,我们给出TARGET
自己的规则并理顺先决条件:
The first three lines and the clean
rule are all right, we'll ignore those. Now we give TARGET
its own rule and straighten out the prerequisites:
all: $(TARGET)
$(TARGET): $(SOURCES)
$(CC) $(SOURCES) -o $(TARGET)
现在,我们将all
设为PHONY(因为它实际上并没有创建名为"all"的文件),并引入自动变量以使TARGET
规则更健壮和更少冗余:
Now we make all
PHONY (since it doesn't really make a file called "all"), and introduce automatic variables to make the TARGET
rule more robust and less redundant:
.PHONY: all
all: $(TARGET)
$(TARGET): $(SOURCES)
$(CC) $< -o $@
如果您的代码库变得更复杂,还有很多东西要学习,但是现在就可以了.
There's more to learn if your codebase gets more complicated, but that'll do for now.
这篇关于Makefile错误:无规则可作为目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!