1

好吧,我从来没有掌握过make和makefile。我试过阅读手册页但没有运气。所以我来了:L

我有一堆文件开始在一个文件中变得非常不受管理。(我正在尝试制作一个操作系统)并且我想尝试将这些文件拆分为单独的子目录(参见下面的结构),然后告诉 make 将这些文件“制作”成他们的 .o 文件,将它们移动到另一个单独的子目录,最后得到一个内核文件。(抱歉,如果这听起来很复杂,希望结构有助于使事情更清晰)

所以这是我想要的结构树:

                                  Parent directory 
                           ( where the makefile will be )
                                          |
                                          |
     -------------------------------------------------------------------------
     |                 |                  |                |                 |
  Header SubDir      Source SubDir      ASM SubDir      Obj SubDir        Kern SubDir
(Header files )     (Source Files)     (Assembly Files)  (Object Files)   (Kernel File)

这是我当前的makefile:

C_SOURCES= main.c
S_SOURCES= boot.s
C_OBJECTS=$(patsubst %.c, obj/%.o, $(C_SOURCES))
S_OBJECTS=$(patsubst %.s, obj/%.o, $(S_SOURCES))
CFLAGS=-nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders
LDFLAGS=-Tlink.ld -melf_i386 --oformat=elf32-i386
ASFLAGS=-felf

all: kern/kernel

.PHONY: clean
clean:
-rm -f kern/kernel

kern/kernel: $(S_OBJECTS) $(C_OBJECTS)
ld $(LDFLAGS) -o $@ $^

$(C_OBJECTS): obj/%.o : %.c 
gcc $(CFLAGS) $<

vpath %.c source

$(S_OBJECTS): obj/%.o : %.s
nasm $(ASFLAGS) $<

vpath %.s asem

它现在吐出这个错误:

ld -Tlink.ld -melf_i386 --oformat=elf32-i386 -o kern/kernel obj/boot.o obj/main.o
ld: cannot find obj/boot.o: No such file or directory
ld: cannot find obj/main.o: No such file or directory

提前感谢您的帮助!

杰米。

4

1 回答 1

0

Let's take this in stages:

all: $(SOURCES) link

You don't have to build the sources, so let's leave that out. And what you really want to build is kern/kernel, so let's use that instead of the abstract "link:

all: kern/kernel

kern/kernel:
    ld $(LDFLAGS) -o kernel $(SOURCES)

But you want to link the object files, not the source files, and produce kernel in kern/, not in the parent directory (where I assume you will be running make):

kern/kernel: $(OBJECTS)
    ld $(LDFLAGS) -o $@ $^

And what are the objects? Well, I presume the sources are the .s files, and the objects have the same names with a different suffix, and in a different location:

SOURCES=boot.s main.s monitor.s common.s descriptor_tables.s isr.s interrupt.s gdt.s timer.s kheap.s paging.s

OBJECTS=$(patsubst %.s,Obj/%.o,$(SOURCES))

And this is how you make an object:

$(OBJECTS): Obj/%.o : %.s
    nasm $(ASFLAGS) $<

vpath %.s Src

(That last line is so that Make will know where to find the sources.)

The flags look good. And here's clean:

.PHONY: clean
clean:
    -rm -f Obj/*.o kern/kernel

You're trying to do a lot at once, so this probably won't work on the first try. Give it a whirl and let us know the result.

于 2012-04-06T14:41:45.663 回答