What does your Makefile look like? (or, how to build kernel)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
stealther
Posts: 18
Joined: Sun Dec 21, 2008 1:53 am
Location: Ukraine, Kyiv

Re: What does your Makefile look like? (or, how to build kernel)

Post by stealther »

andreaorru wrote:Aren't you linking all the library to your kernel, in that way?
Yes ;) At the moment it's a "hello-world" kernel so there's no way to link at runtime.
User avatar
AndreaOrru
Member
Member
Posts: 50
Joined: Fri Apr 25, 2008 2:50 pm
Location: New York

Re: What does your Makefile look like? (or, how to build kernel)

Post by AndreaOrru »

stealther wrote:
andreaorru wrote:Aren't you linking all the library to your kernel, in that way?
Yes ;) At the moment it's a "hello-world" kernel so there's no way to link at runtime.
Is there a way to (statically) link the kernel only with the library's files which get included?
Close the world, txEn eht nepO
User avatar
stealther
Posts: 18
Joined: Sun Dec 21, 2008 1:53 am
Location: Ukraine, Kyiv

Re: What does your Makefile look like? (or, how to build kernel)

Post by stealther »

Maybe there's some linker switch which omits unreferenced object files (or even contents of object files).
Maybe ld does it automatically.
It's beyond my knowledge database. :lol:
User avatar
AndreaOrru
Member
Member
Posts: 50
Joined: Fri Apr 25, 2008 2:50 pm
Location: New York

Re: What does your Makefile look like? (or, how to build kernel)

Post by AndreaOrru »

I'll wait for Solar or someone else, then =)
Thanks anyway.
Close the world, txEn eht nepO
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: What does your Makefile look like? (or, how to build kernel)

Post by Solar »

andreaorru wrote:I'll wait for Solar or someone else, then =)
:D

That one cracked me up. Thanks for the laugh. :)

I know that, when linking from a linker archive (*.ar), 'ld' only takes those object files actually required to satisfy dependencies, leaving any others out of the resulting executable. Perhaps that's the feature you are looking for.
Every good solution is obvious once you've found it.
User avatar
AndreaOrru
Member
Member
Posts: 50
Joined: Fri Apr 25, 2008 2:50 pm
Location: New York

Re: What does your Makefile look like? (or, how to build kernel)

Post by AndreaOrru »

Solar wrote:I know that, when linking from a linker archive (*.ar), 'ld' only takes those object files actually required to satisfy dependencies, leaving any others out of the resulting executable. Perhaps that's the feature you are looking for.
Any other way? I'd want my Makefile to build and link only what is needed from the library. I think it would be more clean.
Close the world, txEn eht nepO
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: What does your Makefile look like? (or, how to build kernel)

Post by Solar »

I don't understand, really. I understood correctly that you have a (static) function library which is linked with your kernel, and you want in the resulting binary only that part of the library which is actually used. Correct?

Well then, individually compile the translation units (*.c and *.asm files) of your library (resulting in a *.o file each), and put them in a linker archive. Link your kernel with that linker archive, and 'ld' does the selection automatically.

Code: Select all

lib.a: $(LIB_OBJFILES)
        @$(AR) $(ARFLAGS) lib.a $?

kernel: lib.a $(KERN_OBJFILES)
        @$(LD) $(LDFLAGS) -T linker.ld -o kernel.bin $(KERN_OBJFILES) lib.a
Every good solution is obvious once you've found it.
User avatar
AndreaOrru
Member
Member
Posts: 50
Joined: Fri Apr 25, 2008 2:50 pm
Location: New York

Re: What does your Makefile look like? (or, how to build kernel)

Post by AndreaOrru »

Ok, thank you!
Close the world, txEn eht nepO
tharkun
Member
Member
Posts: 51
Joined: Sat Mar 21, 2009 1:29 pm
Location: Ireland

Re: What does your Makefile look like? (or, how to build kernel)

Post by tharkun »

manonthemoon wrote:

Code: Select all

loader.o: src/loader.asm
                    nasm -f elf $< -o $@

# a source code file.  You need one of these for each file in your project
bin/main.o: src/main.cpp
                    $(CC) $(CFLAGS) -o $@ -c $<

bin/kio.o: src/kio.cpp
                    $(CC) $(CFLAGS) -o $@ -c $<
You can actually replace these lines with:

Code: Select all

%.o: %.s:
                    nasm -f elf $< -o $@
%.o: %.c
                    $(CC) $(CFLAGS) -o $@ -c $<
This creates a *.o file for every *.c and *.s file.
It' a great timesaver.
Post Reply