本文介绍了Makefile 不“看到"%.o%.asm规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个项目,想要创建一个好的makefile.在某个时候,我注意到它不适用于多个.asm文件,我做了一些研究并修改了我的文件,因此看起来像这样:

I'm writing a project and wanted to make one, good makefile. At some point I noticed that it doesn't work for multiple .asm files, I did some research and modified my file, so it looks like this:

PROJDIRS := kernel lib
ASMFILES := $(shell find $(PROJDIRS) -type f -name "*.asm")
SRCFILES := $(shell find $(PROJDIRS) -type f -name "*.c")
HDRFILES := $(shell find $(PROJDIRS) -type f -name "*.h")

ASMOBJCT := $(patsubst %.asm,%.o,$(ASMFILES))
OBJFILES := $(patsubst %.c,%.o,$(SRCFILES))
TSTFILES := $(patsubst %.c,%_t,$(SRCFILES))

DEPFILES    := $(patsubst %.c,%.d,$(SRCFILES))
TSTDEPFILES := $(patsubst %,%.d,$(TSTFILES))

WARNINGS := -Wall -Wextra -pedantic

NASM=nasm
CC=/usr/local/cross/bin/i686-elf-gcc
CFLAGS=-nostdlib -nostdinc -ffreestanding -O2 $(WARNINGS) -masm=intel -Iinclude -std=c99
LDFLAGS=-T link.ld -nostdlib -ffreestanding -O2 -lgcc
ASFLAGS=-felf

all: $(OBJFILES) link

clean:
    -rm kernel/*.o
    -rm kernel/hal/*.o
    -rm _bin_/*.elf

link:
    /usr/local/cross/bin/i686-elf-gcc $(LDFLAGS) $(OBJFILES) $(ASMOBJCT) -o _bin_/kernel.elf

%.o: %.c
    $(CC) $(CFLAGS) $< -c -o $@

%.o: %.asm
    $(NASM) $(ASFLAGS) $< -o $@

但是由于某种原因,最后一个不执行,因为链接器抛出错误,找不到所需的目标文件.我不知道我在做什么错.您知道如何解决这个奇怪的问题吗?

But for some reason the last one does not execute, as a result linker throws an error that required object files are not found. I have no idea what am I doing wrong. Do you have any idea how do I fix this weird issue?

推荐答案

您永远不会告诉make来构建它们.

You never tell make to build them.

您的默认规则是 all:$(OBJFILES)链接,它告诉您在 $(OBJFILES) link 目标中构建所有内容.这不包含在 $(ASMOBJCT)中的任何内容,因此当 link 使用它们时,它们将不存在.

Your default rule is all: $(OBJFILES) link which tells to build everything in $(OBJFILES) and the link target. This doesn't include anything in $(ASMOBJCT) so when link goes to use them they don't exist.

您可以通过将 link 目标的实际先决条件放在 link:行上来解决此问题,如下所示:

You can fix this by putting the actual prereqs for the link target on the link: line like so:

link: $(OBJFILES) $(ASMOBJCT)

,然后在链接行上使用 $ ^ (所有先决条件),而不是 $(OBJFILES)$(ASMOBJCT).

and then use $^ (all prereqs) instead of $(OBJFILES) $(ASMOBJCT) on the linking line.

然后您可以从 all 目标先决条件列表中删除 $(OBJFILES),因为您不再需要它了.

You could then drop $(OBJFILES) from the all target prereq list since you won't need it there anymore.

您还可以在 all 目标行上,然后在 link中,将 link 替换为 _bin_/kernel.elf :目标行,然后在链接行上使用 -o'$ @'(规则目标),而不是将其写出.

You could also replace link with _bin_/kernel.elf on the all target line and then in the link: target line and then use -o '$@' (rule target) on the linking line instead of writing it out.

这篇关于Makefile 不“看到"%.o%.asm规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:54