问题
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?
回答1:
You never tell make to build them.
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.
You can fix this by putting the actual prereqs for the link
target on the link:
line like so:
link: $(OBJFILES) $(ASMOBJCT)
and then use $^
(all prereqs) instead of $(OBJFILES) $(ASMOBJCT)
on the linking line.
You could then drop $(OBJFILES)
from the all
target prereq list since you won't need it there anymore.
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.
来源:https://stackoverflow.com/questions/25435416/makefile-does-not-see-o-asm-rule