没有规则生成目标文件
我需要帮助,只是因为我需要帮助:
SRC=src/main.c
OBJ_PATH=bin
OBJS := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
all:$(OBJ_PATH)/target.exe
$(OBJ_PATH)/target.exe: $(OBJ_PATH) $(OBJS)
$(CC) $(OBJS) -o $(OBJ_PATH)/target.exe
$(OBJ_PATH):
mkdir -p bin
$(OBJ_PATH)/%.o:%.c
mkdir -p bin
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
.PHONY: clean
clean:
rm -f $(OBJ_PATH)/*
运行时它会给出以下内容: make: *** 没有规则可以创建“bin/target.exe”所需的目标“bin/main.o”。停止。
如果我将对象保留在与 c 文件相同的文件夹中,它就可以工作。 我只是需要一些帮助,也许这是我没有看到的简单事情。 谢谢你们。
I need help, simply because I need help:
SRC=src/main.c
OBJ_PATH=bin
OBJS := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))
all:$(OBJ_PATH)/target.exe
$(OBJ_PATH)/target.exe: $(OBJ_PATH) $(OBJS)
$(CC) $(OBJS) -o $(OBJ_PATH)/target.exe
$(OBJ_PATH):
mkdir -p bin
$(OBJ_PATH)/%.o:%.c
mkdir -p bin
$(CC) -c $(CFLAGS) $(CPPFLAGS) lt; -o $@
.PHONY: clean
clean:
rm -f $(OBJ_PATH)/*
when running it gives this:
make: *** No rule to make target 'bin/main.o', needed by 'bin/target.exe'. Stop.
If I leave the objects in the same folder as the c files, it works.
I just need some help, maybe it is something simple that I am not seeing.
Thanks guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是错误的:
当 make 想要构建一个文件
bin/main.o
时。它与目标模式bin/%.o
匹配,词干为main
(与%
匹配的部分)。将先决条件模式%.c
替换为main
后,make 将尝试查找先决条件main.c
。但是,该文件不存在,并且 make 不知道如何创建它。因此,该模式不匹配,并且 make 尝试找到一种不同的模式来构建
bin/main.o
,但没有,所以 make 表示无法构建该目标。您需要制定模式规则:
以便当 make 替换先决条件模式中的
%
时,它会生成存在的src/main.c
,并且这将起作用。你的 makefile 还存在其他问题;例如,这是一个坏主意:
您永远不会(*)想要使用像
$(OBJ_PATH)
这样的目录作为简单的先决条件。另外,这个:
可以更容易地写成:
(*)确实在非常具体的情况下,将目录作为先决条件可能很有用,但这种情况很少见,除非您完全理解为什么它通常不是什么,否则您不应该这样做你想要的。
This is wrong:
When make wants to build a file
bin/main.o
. It matches the target patternbin/%.o
, with a stem ofmain
(the part that matches the%
). After replacing the prerequisite pattern%.c
withmain
, make will try to find the prerequisitemain.c
.But, that file doesn't exist and make has no idea how to create it. So, that pattern doesn't match and make tries to find a different pattern that will build
bin/main.o
, but there isn't one, so make says there's no way to build that target.You need to make your pattern rule:
so that when make replaces
%
in the prerequisite pattern it yieldssrc/main.c
, which exists, and this will work.There are other problems with your makefile; for example this is a bad idea:
You never(*) want to use a directory like
$(OBJ_PATH)
as a simple prerequisite.Also, this:
can be more easily written:
(*) There can indeed be very specific situations where having a directory as a prerequisite can be useful but they are rare and you shouldn't do it unless you fully understand why it's usually not what you want.