views:

258

answers:

2

I have a assembly file and a c file compiled to .o files (start.o and main.o) and is trying to link them with ld. I'm using this command:

ld -T link.ld -o kernel.bin start.o main.o

where link.ld is a linker script, but when I run it, i get this error:

start.o:start.o:(.text+0x2d): undefined reference to `_main'

in the assembly file, I call the c file with this function:

stublet:
extern _main
call _main

jmp $

Anybody can see what's wrong?

+2  A: 

Some compilers (like GCC for Linux) don't add _ by default to C library exports. Try nm main.o to see the actual reference name. It might be main rather than _main.

Mehrdad Afshari
A: 

Some linkers are sensitive to the order that object files or libraries appear on the command line - try swapping the order of your two object files.

I should also point out that the C standard makes no guarantee that main() is a function - in fact, C programs are explicitly forbidden to call main.

anon