Search code examples
windowsgcclinkerfreestanding

undefined reference to `WinMain` while compiling my own kernel


I've started writing my very own kernel and am developing on Windows 10.

I am using the following tools:

  • gcc 8.1.0 elf x86_64 for compiling my C code, and for linking.
  • nasm 2.14.02 for compiling my assembly code.

I am using the following command to build my freestanding kernel code:

gcc -c -m64 common/src/kernel.c -o common/build/kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra`

I am using the following command for compiling the root assembly code:

nasm -f elf64 targets/x86_64/src/main.asm -o targets/x86_64/build/main.o

Finally I link the object files together:

gcc -o dist/x86_64/main.bin -ffreestanding -O2 targets/x86_64/build/main.o common/build/kernel.o -lgcc

I haven't a clue why, but the following error is stopping the linking process from working:

... undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

This doesn't make sense to me because my compilation and linking is in freestanding mode and I am not attempting to use any windows APIs.

How do I fix this?


If it matters, here is my very simple code:

main.asm:

EXTERN kernel_main
call kernel_main
jmp $

; padding and magic number
times 510-($-$$) db 0
dw 0xaa55

kernel.c:

void kernel_main()
{
  // empty
}

Edit: It looks like I might need the x86_64-elf-gcc compiler

Edit 2: I tried the x86_64-w64-mingw32-gcc compiler, same error occurs


Solution

  • I was missing the -nostdlib flag on the linker. This seems to have solved the problem.