I recently decided to haphazardly (my biggest mistake) write an operating system kernel. I used the OSDev Wiki Barebones as a base for it. Here's what I used to compile it. Have fun!
nasm -f elf -o loader.o loader.s
gcc -o kernel.o -c kernel.c -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs
ld -T linker.ld kernel.o loader.o -o kernel.elf
Now on to the next step of this kernel. Make sure you know the C language in depth, especially pointers, memory management, data structures, and algorithms.