Page 1 of 1

Compiler Warning

Posted: Fri Aug 07, 2009 1:43 am
by Abunada
Hi

Does anybody know how to disable the STUPID warning gcc generates whenever I declare main as being void and taking no parameters. It is so annoying, its been standing on my nerves for ages. ](*,)

Here is the warning:

Code: Select all

../Kernel/main.c:8: warning: return type of ‘main’ is not ‘int’
Here is how I compile my kernel main:

Code: Select all

gcc -c ../Kernel/main.c -o main.o -Wall -I"../Kernel" -I"../Library"  -I"../Devices" -fno-stack-protector -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -nostdlib -fno-builtin
Cheers, Abdullah

Re: Compiler Warning

Posted: Fri Aug 07, 2009 2:06 am
by Combuster
GCC is not stupid, you are violating the C standard (main must return an int).

ever tried renaming main(...) to kmain(...) ? :wink:

Re: Compiler Warning

Posted: Fri Aug 07, 2009 2:31 am
by Solar
The C99 Standard wrote: 5.1.2.2.1 Program startup

The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
Actually, in another part of the standard, they blurb about "if the return type of main() is not compatible with int". But main() implies more than that - for example, the execution of all functions registered with atexit(), which your kernel's main function probably doesn't do.

So, yes, the canon way to do it is to call the main function of a kernel kmain().

Re: Compiler Warning

Posted: Fri Aug 07, 2009 3:54 am
by Creature
Or you could just use int, drop a

Code: Select all

return WhateverBogusValueYouWant;
at the end or wherever you need to return and absolutely ignore the return value in the Assembly code (of your bootloader or GRUB header which calls main or whatever you want).

Re: Compiler Warning

Posted: Fri Aug 07, 2009 4:43 am
by Solar
Yes, that's the "Ugly Workaround" option. ;-)

Re: Compiler Warning

Posted: Fri Aug 07, 2009 6:11 am
by Love4Boobies
Actually everyone here is wrong.

The C standard says that main() can be declared as either

int main(void);
int main(int argc, char *argv);

or in an implementation-speicific way. If, for some unknown reason to me, you want main() to be void instead of int just compile in a free-standing environment (which is also defined by the C standard) - i.e., use the -ffreestanding switch with GCC. I would like to note that it makes no difference wether you have void or int as there is no overhead - you kernel should never get to the end of the main/kmain function. Even if it did, "return 0" would just change the value of one register.

Also, the "return 0" is uneeded. The value is implied when main reaches the "}".

Re: Compiler Warning

Posted: Fri Aug 07, 2009 6:57 am
by gravaera
I'd really hate to burst your chops, Love4Boobies, but Solar's quote had it covered. :wink:

Re: Compiler Warning

Posted: Fri Aug 07, 2009 7:33 am
by Love4Boobies
gravaera wrote:I'd really hate to burst your chops, Love4Boobies, but Solar's quote had it covered. :wink:
Yeah, I must've missed that one. But I provided the free-standing environment solution.

Re: Compiler Warning

Posted: Fri Aug 07, 2009 8:38 am
by Abunada
Thanks guys for the reply,

I am completely aware of the renaming solution; however, I just wondered whether it can be done with the function called "main", I just feel something is wrong when I see a kmain there!! As mentioned above the return WhateverBogusValueYouWant is an even uglier solution, I hate doing things in my code that does not make sense. I understand that the prototype of main can be implementation dependent that's why I asked. The sort of thing I was looking for is the -ffreestanding option Love4Boobies mentioned, Thanks a lot mate I am a lot more comfortable now !!! 8)

Re: Compiler Warning

Posted: Fri Aug 07, 2009 9:36 am
by Solar
Love4Boobies wrote:...or an implementation-speicific way.
This means the implementation of the compiler (and/or its C runtime), mind you. ;-)

Re: Compiler Warning

Posted: Fri Aug 07, 2009 9:45 am
by Abunada
Oh! :shock: , Thanks for the point out Solar.

Re: Compiler Warning

Posted: Fri Aug 07, 2009 3:27 pm
by Love4Boobies
That's true. And following that rule, the GCC free-standing environment allows you to define main() however you like (design choice).