没有规则生成目标文件

发布于 2025-01-11 21:40:08 字数 585 浏览 4 评论 0原文

我需要帮助,只是因为我需要帮助:

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 技术交流群。

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

发布评论

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

评论(1

落在眉间の轻吻 2025-01-18 21:40:08

这是错误的:

$(OBJ_PATH)/%.o: %.c

当 make 想要构建一个文件 bin/main.o 时。它与目标模式 bin/%.o 匹配,词干为 main(与 % 匹配的部分)。将先决条件模式 %.c 替换为 main 后,make 将尝试查找先决条件 main.c

但是,该文件不存在,并且 make 不知道如何创建它。因此,该模式不匹配,并且 make 尝试找到一种不同的模式来构建 bin/main.o,但没有,所以 make 表示无法构建该目标。

您需要制定模式规则:

$(OBJ_PATH)/%.o: src/%.c

以便当 make 替换先决条件模式中的 % 时,它会生成存在的 src/main.c ,并且这将起作用。

你的 makefile 还存在其他问题;例如,这是一个坏主意:

$(OBJ_PATH)/target.exe: $(OBJ_PATH) $(OBJS)

您永远不会(*)想要使用像 $(OBJ_PATH) 这样的目录作为简单的先决条件。

另外,这个:

OBJS := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))

可以更容易地写成:

OBJS := $(patsubst src/%.c,$(OBJ_PATH)/%.o,$(SRC))

(*)确实在非常具体的情况下,将目录作为先决条件可能很有用,但这种情况很少见,除非您完全理解为什么它通常不是什么,否则您不应该这样做你想要的。

This is wrong:

$(OBJ_PATH)/%.o: %.c

When make wants to build a file bin/main.o. It matches the target pattern bin/%.o, with a stem of main (the part that matches the %). After replacing the prerequisite pattern %.c with main, make will try to find the prerequisite main.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:

$(OBJ_PATH)/%.o: src/%.c

so that when make replaces % in the prerequisite pattern it yields src/main.c, which exists, and this will work.

There are other problems with your makefile; for example this is a bad idea:

$(OBJ_PATH)/target.exe: $(OBJ_PATH) $(OBJS)

You never(*) want to use a directory like $(OBJ_PATH) as a simple prerequisite.

Also, this:

OBJS := $(addprefix $(OBJ_PATH)/, $(addsuffix .o, $(notdir $(basename $(SRC)))))

can be more easily written:

OBJS := $(patsubst src/%.c,$(OBJ_PATH)/%.o,$(SRC))

(*) 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.

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