Page 1 of 1

Calling _main in Bran's Kernel Tutorial

Posted: Sat Dec 19, 2009 3:42 am
by Tempestum
Hi everyone,

Normally I don't post much on the forums, but I couldn't find a solution for this problem (or, actually, I am not sure if I am doing this right), so I decided to ask for help here.

I am following Bran's Kernel Tutorial, and when it comes to the section where I have to start using C source files, and calling the main function in the assembly code, I have a problem.

As Brandon suggested in his tutorial, calling the main function from the C source file (main.c) should be done by adding the following lines immediately after 'stublet:'

Code: Select all

extern _main
call _main
The problem is, when I start compiling and linking the whole thing. My makefile returns an error saying:
kernel.o:kernel.o(.text.0x2d) undefined reference to '_main'
I changed '_main' to 'main' in my code, and then it didn't came up with errors anymore.

However, I don't know if I have really 'solved' the problem. Though it does not come up with errors anymore, is using 'main' in assembly a good thing to do? Brandon said that GCC (I use version 4.3.3 by the way) automatically added an underscore before every function in C, so I guess my GCC compiler does not do that? Or is there something else I need to fix?

Thanks in advance,
~Temp.

Re: Calling _main in Bran's Kernel Tutorial

Posted: Sat Dec 19, 2009 3:48 am
by zity
GCC will use underscores depending on the configuration, if you're using the flag -fno-leading-underscore gcc won't add underscores to the function name.

But your solution is pefectly fine :)

Re: Calling _main in Bran's Kernel Tutorial

Posted: Sat Dec 19, 2009 4:05 am
by Grunt
Tempestum wrote:Brandon said that GCC (I use version 4.3.3 by the way) automatically added an underscore before every function in C, so I guess my GCC compiler does not do that?
You should have tried that first.

If that doesn't fix it, then I guess it's the linker script.

Re: Calling _main in Bran's Kernel Tutorial

Posted: Sat Dec 19, 2009 4:18 am
by pcmattman
To build upon zity's answer...

Some (mainly older) versions of GCC (and some specific compile targets as well) have different underscore rules built into the compiler. Your best bet for the most portable (at least, across GCC versions and targets ;) ) solution is to use -fno-leading-underscore and ensure no underscores are used in your assembly code.

Re: Calling _main in Bran's Kernel Tutorial

Posted: Sat Dec 19, 2009 4:26 am
by Tempestum
Grunt wrote:
Tempestum wrote:Brandon said that GCC (I use version 4.3.3 by the way) automatically added an underscore before every function in C, so I guess my GCC compiler does not do that?
You should have tried that first.
I actually did try that first:
I changed '_main' to 'main' in my code, and then it didn't came up with errors anymore.
I was asking whether this was actually 'normal', and if it wouldn't get me into trouble later. :wink:

Anyway, thanks everyone! I've just got GRUB working on my floppy and it seems my kernel is working fine.