1

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

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)

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!

Jamie.

  • Related: [GNU `make` documentation](http://www.gnu.org/software/make/manual/make.html) – bitmask Apr 06 '12 at 13:56
  • Possible Duplicate of http://stackoverflow.com/questions/1139271/makefiles-with-source-files-in-different-directories – Pavan Manjunath Apr 06 '12 at 13:56
  • Pavan Manjunath No this isn't a duplicate, it's slightly related to that post, but I'm doing it completely differently. bitmask How do I write the makefile to compile and link the source files that are in different subdirectories? –  Apr 06 '12 at 14:04
  • You never set `OBJECTS` -- you probably want `OBJECTS=$(C_OBJECTS) $(S_OBJECTS)` – Chris Dodd Apr 06 '12 at 16:37
  • @ChrisDodd Thanks, any idea's in why it's still spitting out the new error above? –  Apr 06 '12 at 21:08

1 Answers1

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.

Beta
  • 96,650
  • 16
  • 149
  • 150
  • The sources are a mix of .s and .c so I will guess I change the SOURCES= boot.s... etc to the relevent suffix? –  Apr 06 '12 at 14:54
  • @JamieEdwards, It would have been good to mention that in your question. The easiest way is to have `C_SOURCES` and `S_SOURCES`, and also `C_OBJECTS` and `S_OBJECTS`, and a rule for compiling `c` files. – Beta Apr 06 '12 at 15:22
  • My sincerest apologise. I should have put the suffix's of the files in :L noobish thing to do really. –  Apr 06 '12 at 15:27
  • Just updated OP with my new makefile, and it's spitting out error's which I have also edited in. Any ideas? –  Apr 06 '12 at 15:41
  • Use `$(S_OBJECTS)` instead of `$(OBJECTS)`. – Matt Eckert Apr 06 '12 at 16:06
  • @JamieEdwards, use `kern/kernel: $(S_OBJECTS) $(C_OBJECTS)`. – Beta Apr 06 '12 at 16:20
  • I did as you said, and it spits out another error, although I think we're almost there! But I have updated the OP with new errors, Do I now have to make another $() for the headers too? –  Apr 06 '12 at 16:40
  • @JamieEdwards, try `gcc $(CFLAGS) -IHeader $<`. – Beta Apr 06 '12 at 17:08
  • According to make, the make file isn't putting the object files into the 'obj' directory. Any ideas? –  Apr 07 '12 at 17:45
  • @JamieEdwards, your `$(C_OBJECTS)` command is wrong. It should be `gcc $(CFLAGS) -IHeader $< -o $@` (sorry I didn't notice that when I was thinking about the header problem). Now about finding the header files; confirm that `Header/monitor.h` is there, then try `gcc -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -IHeader src/main.c -o Obj/main.o` from the command line, and see what happens. – Beta Apr 07 '12 at 20:37
  • @JamieEdwards, in my previous comment, each gcc command should have a `-c` flag as well, but I'm having trouble editing the comment. – Beta Apr 07 '12 at 20:45
  • @Beta I actually figured out that problem and it's now finding the header files. Only problem now is that the makefile isn't transferring the resulting object files into the obj directory, which is preventing ld from linking the files into one kernel file. I've updated OP for you to see what is wrong. –  Apr 07 '12 at 21:30
  • @Beta - Scratch what I just wrote above! I finally have my makefile doing what I want with yourself and everyone else's help! Thank you so much for your patience, and help! –  Apr 07 '12 at 21:39