as the tutorial advances to the point where you combine the boot_sector code with the kernel code to boot the OS , the amount of terminal commands to compile and execute the OS-image was fairly high.
So the tutorial (fortunately) included a full section about Automating builds with Make
So i decided to break up all of the OS related codes into three directories : boot, drivers, kernel
--Code(here will be the makefile)
--boot:
--some necessary .asm files
--kernel:
--all kernel related files are here (such as kernel.c)
--drivers:
--all driver related files are here
and here is my Makefile so far :
Code: Select all
# Automatically generate lists of sources using wildcards.
VPATH=./boot:./kernel:./drivers
C_SOURCES = $( wildcard kernel/*.c drivers/*.c )
HEADERS = $( wildcard kernel/*.h drivers/*.h )
# TODO : Make sources dep on all header files.
# Convert the *. c filenames to *. o to give a list of object files to build
OBJ = ${C_SOURCES:.c =.o}
# Defaul build target
all: os-image
# Run bochs to simulate booting of our code.
run: all
bochs
# This is the actual disk image that the computer loads
# which is the combination of our compiled bootsector and kernel
os-image : boot/boot_sect_c.bin kernel.bin
cat $^ > os-image
# This builds the binary of our kernel from two object files :
# - the kernel_entry , which jumps to main () in our kernel
# - the compiled C kernel
kernel.bin : kernel/kernel_entry.o ${OBJ}
ld -o $@ -Ttext 0x1000 $^ --oformat binary
# Generic rule for compiling C code to an object file
# For simplicity , we C files depend on all header files .
%.o : %.c ${HEADERS}
gcc -ffreestanding -c $< -o $@
# Assemble the kernel_entry.
%.o : %.asm
nasm $< -f elf -o $@
%.bin : %.asm
nasm $< -f bin -I 'boot/' -o $@
clean:
rm -fr *.bin *.dis *.o os-image
rm -fr kernel/*.o boot/*.bin drivers/*.o
Code: Select all
make run
Code: Select all
nasm boot/boot_sect_c.asm -f bin -I 'boot/' -o boot/boot_sect_c.bin
nasm kernel/kernel_entry.asm -f elf -o kernel/kernel_entry.o
ld -o kernel.bin -Ttext 0x1000 kernel/kernel_entry.o --oformat binary
ld: warning: cannot find entry symbol _start; defaulting to 0000000000001000
[color=#FF0000]kernel/kernel_entry.o:(.text+0x1): undefined reference to `main'
make: *** [kernel.bin] Error 1[/color]
Any help would be greatly appreciated , Thanks