Undefined link to main ld
I am trying to link files - a c file containing the main function and an asm file that just goes to the main one.
I have mingw installed. My files:
//kernel.c
void some_function(){
}
void main(){
char* video_memory = (char*) 0xb8000;
*video_memory = 'X';
some_function();
}
;kernel_entry.asm
[bits 32]
[extern main]
call main
jmp $
The commands I call to build:
gcc -ffreestanding -c kernel.c -o kernel.o
nasm kernel_entry.asm -f elf -o kernel_entry.o
ld -o kernel.bin -Ttext 0x1000 kernel_entry.o kernel.o
The errors I am getting:
kernel_entry.o:(.text+0x1): undefined reference to `main'
kernel.o:kernel.c:(.text+0xf): undefined reference to `__main'
EDIT:
Which teams are working:
ld -r -o kernel.out -Ttext 0x1000 kernel.o
objcopy -O binary -j .text kernel.out kernel.bin
When I try to run ld with -r I get the error:
ld: Relocatable linking with relocations from format elf32-i386 (kernel_entry.o)
to format pe-i386 (kernel.bin) is not supported
EDIT2: I have had better results using these commands:
gcc -ffreestanding -c kernel.c -o kernel.o
nasm kernel_entry.asm -f win32 -o kernel_entry.o
ld -r -o kernel.out -Ttext 0x1000 kernel_entry.o kernel.o
objcopy -O binary -j .text kernel.out kernel.bin
Files linked successfully, but on startup the main call is not called. Also tried with coff format, it is also referenced but Bochs keeps reloading.
source to share
The first mistake is that in C the function is called _name
, so you cannot call main
as such, you must call _main
. In TASM you can set the calling convention, so assmebler can automatically call the correct function, I don't know if there is such a function for nasm.
The second problem is probably due to the fact that you are calling the linker directly. In this case, you must specify the C launcher, which is usually added by the compiler to the linker options. I usually think of it as a file named like crt0
. If you write your own startup code, you must provide it yourself. This module installs the environment for C from a specific OS entry point. I assume this is missing in your project.
source to share