I'm now posting the second semi-complete part of my operating system makefile. The kernel itself is currently incomplete, so don't be surprised by the small size of the makefile. When the kernel expands, it will follow suit.

OFLAGS = -Ofast
DEBUGFLAGS = -g -g3 -ggdb3
TARGET = i686-elf
CC = gcc
INCDIR = -I kernel/include/
KERNEL = opal101
CFLAGS = -ffreestanding -Wall -Wextra -fno-exceptions -funsigned-char
LDFLAGS = -T linker.ld -Ofast -nostdlib -lgcc
AS = as
VPATH = kernel/arch/$(TARGET) $(shell find . -type d ! -name "arch"*)
include kernel/arch/$(TARGET)/Makefile
include kernel/drivers/vga/Makefile
include kernel/lib/Makefile
.PHONY: help all clean iso run debug
        @echo "make: Usage: make [target]";\
        echo "Targets [run | iso | all | cleani | debug]"
debug: all
        qemu-system-i386 --kernel $(KERNEL).bin -S -s& \
        gdb-multiarch -s $(KERNEL).bin
        qemu-system-i386 -cdrom $(KERNEL).iso
iso: all
        mkdir -p $(KERNEL)/boot/grub;\
        cp kernel/arch/i686-elf/stage2_eltorito $(KERNEL)/boot/grub;\
        cp $(KERNEL).bin $(KERNEL)/boot;\
        printf "default 0\n" >>menu.lst;\
        printf "title %s\n" $(KERNEL) >>menu.lst;\
        printf "kernel /boot/%s.bin" $(KERNEL) >>menu.lst;\
        mv menu.lst $(KERNEL)/boot/grub;\
        genisoimage -R -b boot/grub/stage2_eltorito -no-emul-boot \
        -boot-load-size 4 -boot-info-table -o $(KERNEL).iso $(KERNEL)
all: $(KERNEL).bin

        $(TARGET)-$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $(KERNEL).bin
        $(TARGET)-$(CC) $(CFLAGS) -c $< -o $@ $(INCDIR)
$(ASM_OBJECTS): %.o:%.s
        $(TARGET)-$(AS) $< -o $@
        rm -rf *.o *.bin *.iso opal101/

I will show the kernel/lib/Makefile. All the rest are almost the same.

C_OBJECTS := $(C_OBJECTS) string.o

NOTE: I am not willing to use auto dependency generators because I found them very complex to learn and use, so please don't ask me to use them.


1 Answer 1


Disclaimer: I'm not experienced with this field, but this question definitely needs some activity, so this is my attempt at an answer. If there's anything that you want me to cover that I don't mention here, just tell me about it and I'll try my best to see what I can do.


I find that some of your rule indentation is a bit excessive, and makes your code a little harder to read. Right now you're indenting your rules by tabs with a width of eight spaces. I'd recommend that you indent by tabs with a width of four, like this:

some_rule : file.c
    do_something file.c -foo -bar

You also seem to have a lack of blank lines between your rules as well. I'd highly recommend including at least one blank line between each rule, and a comment, if necessary:

# ...
some_rule1 : file.c

some_rule2 : file.c

Finally, you're inconsistent in your colon (:) placement in some places. For example, sometimes you place your colons like this:

all: $(KERNEL).bin

Other times, you place them like this:


I prefer the following style, but as long as you're, again, consistent, any of the above or below are fine:

some_rule : ...
  • \$\begingroup\$ As I recall, Makefiles expect your rules to be indented with a tab - I don't recall with how many, but I know the markdown editor will replace the tabs with spaces. That might be what you're seeing. \$\endgroup\$ Oct 4, 2015 at 22:21
  • \$\begingroup\$ @Dannnno Ah. Edited. \$\endgroup\$ Oct 4, 2015 at 22:22
  • \$\begingroup\$ well I think nothing is wrong but with style thank you \$\endgroup\$
    – u185619
    Oct 5, 2015 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.