使用make文件来编译在不同的目录中的文件 [英] Using a make file to compile files in separate directories
问题描述
OK,我从来没有能掌握化妆,和生成文件。我试图通过与没有运气的联机帮助页阅读。所以,我来到这里:L
OK, I have never been able to grasp make, and makefiles. I've tried reading through the manpages with no luck. So I have come here :L
我有一堆开始变得非常不管理在一个文件中的文件。 (我试图使一个OS),我想尝试和拆分这些文件到不同的子目录(请参阅下面的结构),然后告诉做出'让'的文件到自己的.o文件,将它们转移到另一个独立的子目录,终于结束了一个内核文件。 (很抱歉,如果这听起来很复杂,希望结构将有助于使事情更清晰)
I have a bunch of files that are starting to get very un-managed in one file. ( I'm trying to make an OS ) And I want to try and split these files into separate sub-directories ( see structure below ) and then tell make to 'make' the files into an their .o files, moving them into another separate sub-directory and finally ending up with a kernel file. ( Sorry if that sounds complicated, hopefully the structure will help make things clearer )
所以这是我预期的结构树:
So this is my intended structure tree:
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:
This is my current 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
现在它吐出这个错误:
It's now spitting out this error:
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
感谢提前任何帮助!
Thanks for any help in advance!
杰米。
推荐答案
让我们在这个阶段:
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)
但要在的目标文件的,不是源文件链接,并在克恩/ <产生
内核
/ code>,而不是在父目录(这里我假设你将运行制作
)
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 $@ $^
和哪些对象?好吧,我presume来源是 .S
文件和对象有不同的后缀相同的名称,并在不同的位置:
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.
这篇关于使用make文件来编译在不同的目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!