Makefile 错误:没有规则可生成目标

发布于 2024-11-05 01:18:56 字数 335 浏览 1 评论 0 原文

SOURCES = server.c

TARGET = Server

CC = gcc

all: $(SOURCES) $(TARGET) 


$(CC) $(SOURCES) -o $(TARGET) 

clean:


rm -rf $(TARGET) 

上面是我的网络服务器的 Makefile。尽管 server.c 文件位于目录中,但这会出现以下错误

make: *** No rule to make target `Server', needed by `all'.  Stop.

我犯了什么错误以及如何解决它。

SOURCES = server.c

TARGET = Server

CC = gcc

all: $(SOURCES) $(TARGET) 


$(CC) $(SOURCES) -o $(TARGET) 

clean:


rm -rf $(TARGET) 

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温折酒 2024-11-12 01:18:56

我认为你的 makefile 在你的机器和帖子之间的某个地方出现了乱码,但是我认为有一个简单的修复方法可以工作:

all: $(SOURCES)

这将(可能)解决问题并使错误消失 - 如果这就是你想要的那么你可以停止阅读。但是这个makefile仍然有问题,所以我们可以做一些更多的改进。

首先,进行一些调整,使其符合我认为您的 makefile 真正所说的内容:

SOURCES = server.c

TARGET = Server

CC = gcc

all: $(SOURCES) $(TARGET)
    $(CC) $(SOURCES) -o $(TARGET) 

clean:
    rm -rf $(TARGET) 

前三行和 clean 规则都可以,我们将忽略它们。现在我们给 TARGET 自己的规则并理清先决条件:

all: $(TARGET)

$(TARGET): $(SOURCES)
    $(CC) $(SOURCES) -o $(TARGET) 

现在我们制作 all PHONY (因为它并没有真正制作一个名为“all”的文件),并引入自动变量使 TARGET 规则更强大且更少冗余:

.PHONY: all
all: $(TARGET)

$(TARGET): $(SOURCES)
    $(CC) 
lt; -o $@ 

如果您的代码库变得更复杂,则需要学习更多内容,但现在就这样了。

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)

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.

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) 

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) 

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) 
lt; -o $@ 

There's more to learn if your codebase gets more complicated, but that'll do for now.

夏九 2024-11-12 01:18:56

只需执行“make clean”来清理所有链接,然后再次运行 make 即可。一切都应该很好。

just do "make clean" to clean all links, then run make again. Everything should be good.

桃扇骨 2024-11-12 01:18:56

我的问题是我的名称和命令在同一行。确保:确保您使用的是制表符而不是空格。 (无双关语)

之前(破碎)

build: docker build...

之后

build:
        docker build...

My issues was I had the name and the command on the same line. Make: sure you are using tabs and not spaces. (no pun intended)

BEFORE (Broken)

build: docker build...

AFTER

build:
        docker build...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文