Compiler Warning

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Abunada
Posts: 17
Joined: Mon Jun 08, 2009 4:36 am

Compiler Warning

Post 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
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Compiler Warning

Post by Combuster »

GCC is not stupid, you are violating the C standard (main must return an int).

ever tried renaming main(...) to kmain(...) ? :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Compiler Warning

Post 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().
Every good solution is obvious once you've found it.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Compiler Warning

Post 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).
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Compiler Warning

Post by Solar »

Yes, that's the "Ugly Workaround" option. ;-)
Every good solution is obvious once you've found it.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Compiler Warning

Post 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 "}".
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Compiler Warning

Post by gravaera »

I'd really hate to burst your chops, Love4Boobies, but Solar's quote had it covered. :wink:
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Compiler Warning

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Abunada
Posts: 17
Joined: Mon Jun 08, 2009 4:36 am

Re: Compiler Warning

Post 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)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Compiler Warning

Post by Solar »

Love4Boobies wrote:...or an implementation-speicific way.
This means the implementation of the compiler (and/or its C runtime), mind you. ;-)
Every good solution is obvious once you've found it.
Abunada
Posts: 17
Joined: Mon Jun 08, 2009 4:36 am

Re: Compiler Warning

Post by Abunada »

Oh! :shock: , Thanks for the point out Solar.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Compiler Warning

Post by Love4Boobies »

That's true. And following that rule, the GCC free-standing environment allows you to define main() however you like (design choice).
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply