Topic: Question about C++ standards and g++
Author: comeau@panix.com (Greg Comeau)
Date: Fri, 3 Nov 2000 16:37:37 GMT Raw View
In article <3A01882B.D1BD3B82@sensor.com>, Ron Natalie <ron@sensor.com> wrote:
>Greg Comeau wrote:
>> >No, it is required that the implementation issue a diagnostic for this.
>> >I commend G++, this is one of the first to actually do so.
>>
>> Comeau C++ does too. Actually, I thought all compilers did.
>>
>No, unfortunately not. Visual C++ at least documents this misfeature (although
>they don't lablel this or wmain as being Microsoft Specific, although they do
>label the option envp third arg as being so). The "disable microsoft
>abominiations" (/Za) flag doesn't seem to have any affect on this.
I didn't realize that.
>I suspect Comeau beat G++ with the diagnostic. The Spar Compiler also labels void
>main as an anachronism... but makes it non-fatal. This predates the version of
>G++ that said anything. G++ is the only compiler that I know of that makes it
>a fatal error though.
It's fatal with Comeau C++ too, at least in strict mode.
>The amusing thing is when you couple "int main" with the braindead implicit
>return:
>
> int main() { }
>
>yields a diagnostic that main doesn't return anything and it's redefining it
>as void main.
Yes, I've heard that.
- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: comeau@panix.com (Greg Comeau)
Date: Thu, 2 Nov 2000 14:50:12 GMT Raw View
In article <3A002BEA.E8BFC59@sensor.com>, Ron Natalie <ron@sensor.com> wrote:
>Tom Williams wrote:
>>....
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> void main(int argc, char **argv)
>> {
>> puts("Hello World!");
>> exit (0);
>> }
>> bash$ /usr/local/egcs/bin/g++ maintest.cpp -o maintest
>> maintest.cpp:5: `main' must return `int'
>> bash$
>>
>> I'm guessing that this error is due to some C++ standard requiring that
>> main return an int. How can I confirm that this is the case or is this
>> just a buglet in the compiler snapshot?
It's the case and not the latter.
Check out http://www.comeaucomputing.com/techtalk/#voidmain
>No, it is required that the implementation issue a diagnostic for this.
>I commend G++, this is one of the first to actually do so.
Comeau C++ does too. Actually, I thought all compilers did.
- Greg
--
Comeau Computing / Comeau C/C++ "so close" 4.2.44 betas NOW AVAILABLE
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Tom Williams <tom.williams@diversifiedsoftware.com>
Date: Thu, 2 Nov 2000 15:53:11 GMT Raw View
I just wanted to say thanks to all of you that replied to my question,
both on this newsgroup and via private e-mail. I didn't think this
question would stir up so many responses!
Anyway, I'll buy a copy of the C++ standard! :-)
Peace......
Tom
In article <8tnr0u$qag$1@nnrp1.deja.com>,
Tom Williams <tom.williams@diversifiedsoftware.com> wrote:
> Hi! I've just installed the egcs-20001030 snapshot on my Linux box
and
> I tried compiling some C++ code using g++ and I'm getting a strange
> error stating that "main must return an int".
>
> Here is a sample program I compile with g++ and the output:
>
> bash$ cat maintest.cpp
> #include <stdio.h>
> #include <stdlib.h>
>
> void main(int argc, char **argv)
> {
> puts("Hello World!");
> exit (0);
> }
> bash$ /usr/local/egcs/bin/g++ maintest.cpp -o maintest
> maintest.cpp:5: `main' must return `int'
> bash$
>
> I'm guessing that this error is due to some C++ standard requiring
that
> main return an int. How can I confirm that this is the case or is
this
> just a buglet in the compiler snapshot?
>
> Thanks in advance for your time.
>
> Peace.....
>
> Tom Williams
> tom.williams@diversifiedsoftware.com
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting
with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu
]
> [ --- Please see the FAQ before posting. ---
]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html
]
> [ Note that the FAQ URL has changed! Please update your bookmarks.
]
>
>
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@sensor.com>
Date: Thu, 2 Nov 2000 18:40:38 GMT Raw View
Greg Comeau wrote:
> >No, it is required that the implementation issue a diagnostic for this.
> >I commend G++, this is one of the first to actually do so.
>
> Comeau C++ does too. Actually, I thought all compilers did.
>
No, unfortunately not. Visual C++ at least documents this misfeature (although
they don't lablel this or wmain as being Microsoft Specific, although they do
label the option envp third arg as being so). The "disable microsoft abominiations"
(/Za) flag doesn't seem to have any affect on this.
I suspect Comeau beat G++ with the diagnostic. The Spar Compiler also labels void
main as an anachronism... but makes it non-fatal. This predates the version of
G++ that said anything. G++ is the only compiler that I know of that makes it
a fatal error though.
The amusing thing is when you couple "int main" with the braindead implicit
return:
int main() { }
yields a diagnostic that main doesn't return anything and it's redefining it
as void main.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Jack Klein <jackklein@spamcop.net>
Date: Wed, 1 Nov 2000 19:10:27 GMT Raw View
On Wed, 1 Nov 2000 01:20:06 GMT, Tom Williams
<tom.williams@diversifiedsoftware.com> wrote in comp.std.c++:
> Hi! I've just installed the egcs-20001030 snapshot on my Linux box and
> I tried compiling some C++ code using g++ and I'm getting a strange
> error stating that "main must return an int".
>=20
> Here is a sample program I compile with g++ and the output:
>=20
> bash$ cat maintest.cpp
> #include <stdio.h>
> #include <stdlib.h>
>=20
> void main(int argc, char **argv)
> {
> puts("Hello World!");
> exit (0);
> }
> bash$ /usr/local/egcs/bin/g++ maintest.cpp -o maintest
> maintest.cpp:5: `main' must return `int'
> bash$
>=20
> I'm guessing that this error is due to some C++ standard requiring that
> main return an int. How can I confirm that this is the case or is this
> just a buglet in the compiler snapshot?
>=20
> Thanks in advance for your time.
>=20
> Peace.....
>=20
> Tom Williams
> tom.williams@diversifiedsoftware.com
Here is the relevant citation from the section 3.6.1 of the standard:
2 An implementation shall not predefine the main function.=20
This function shall not be overloaded. It shall have a return=20
type of type int, but otherwise its type is implementation=AD
defined.
Violating a clause containing a "shall" is a constraint violation, and
constraint violations require the compiler to issue a diagnostic.
There is no requirement that the compiler abort compilation or refuse
to produce output.
BTW, you may purchase and download a copy of the standard in Adobe PDF
format from ANSI (http://www.ansi.org) for $18.00 US. IMHO, no
professional C++ programmer should be without it.
Jack Klein
--=20
Home: http://jackklein.home.att.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "CC Ghost" <ccghst@erehwon.com>
Date: Wed, 1 Nov 2000 19:14:26 GMT Raw View
Tom Williams wrote in message <8tnr0u$qag$1@nnrp1.deja.com>...
<snip>
>I'm guessing that this error is due to some C++ standard requiring that
>main return an int. How can I confirm that this is the case or is this
>just a buglet in the compiler snapshot?
In the C++ standards document, 3.6.1
"[main]...shall have a return of type int,..."
Link to the C++ standard:
http://webstore.ansi.org/ansidocstore/product.asp?sku=ISO%2FIEC+14882%2D1998
And main link to the ANSI standards store:
http://webstore.ansi.org/ansidocstore/default.asp
It's only $18, well worth picking up.
Since this is also a very basic question, here's some
other links for you:
Welcome to comp.lang.c++
http://www.slack.net/~shiva/welcome.txt
Off-topic posts
http://members.xoom.com/jshiva/offtopic.txt
The comp.lang.c FAQ
http://www.eskimo.com/~scs/C-faq/top.html
The comp.lang.c++ FAQ Lite
http://marshall-cline.home.att.net/cpp-faq-lite/
Alternate C FAQ - gives some tips for platform-
specific problems
http://bit.csc.lsu.edu/tutorial/ten-commandments/c-faq/c-faq-toc.html
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Tom Williams <tom.williams@diversifiedsoftware.com>
Date: Wed, 1 Nov 2000 01:20:06 GMT Raw View
Hi! I've just installed the egcs-20001030 snapshot on my Linux box and
I tried compiling some C++ code using g++ and I'm getting a strange
error stating that "main must return an int".
Here is a sample program I compile with g++ and the output:
bash$ cat maintest.cpp
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv)
{
puts("Hello World!");
exit (0);
}
bash$ /usr/local/egcs/bin/g++ maintest.cpp -o maintest
maintest.cpp:5: `main' must return `int'
bash$
I'm guessing that this error is due to some C++ standard requiring that
main return an int. How can I confirm that this is the case or is this
just a buglet in the compiler snapshot?
Thanks in advance for your time.
Peace.....
Tom Williams
tom.williams@diversifiedsoftware.com
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@sensor.com>
Date: Wed, 1 Nov 2000 15:44:27 GMT Raw View
Tom Williams wrote:
>
> Hi! I've just installed the egcs-20001030 snapshot on my Linux box and
> I tried compiling some C++ code using g++ and I'm getting a strange
> error stating that "main must return an int".
>
> Here is a sample program I compile with g++ and the output:
>
> bash$ cat maintest.cpp
> #include <stdio.h>
> #include <stdlib.h>
>
> void main(int argc, char **argv)
> {
> puts("Hello World!");
> exit (0);
> }
> bash$ /usr/local/egcs/bin/g++ maintest.cpp -o maintest
> maintest.cpp:5: `main' must return `int'
> bash$
>
> I'm guessing that this error is due to some C++ standard requiring that
> main return an int. How can I confirm that this is the case or is this
> just a buglet in the compiler snapshot?
No, it is required that the implementation issue a diagnostic for this.
I commend G++, this is one of the first to actually do so.
Declaring main as anything else has really never been legitimate. It's
just that you could get away with it on most platforms because functions
calling a function returning int which infact were slipped a function
returning void, just got garbage (usually whatever is in some register)
for a return value.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kuyper <kuyper@wizard.net>
Date: Wed, 1 Nov 2000 15:46:57 GMT Raw View
Tom Williams wrote:
>
> Hi! I've just installed the egcs-20001030 snapshot on my Linux box and
> I tried compiling some C++ code using g++ and I'm getting a strange
> error stating that "main must return an int".
Correct.
> I'm guessing that this error is due to some C++ standard requiring that
> main return an int. ...
Good guess. By the way, it's not just "some" C++ standard; it's "the"
C++ standard: ISO/IEC 14882:1998(E).
> ... How can I confirm that this is the case or is this
By examining section 3.6.1p2, which describes main(). "It shall return
int." Implementations are free to accept other argument lists, but not
different return types.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: brahms@mindspring.com (Stan Brown)
Date: Wed, 1 Nov 2000 15:49:49 GMT Raw View
[cc'd to author for speed; please follow up in newsgroup.]
Tom Williams <tom.williams@diversifiedsoftware.com> wrote in
comp.std.c++:
>Hi! I've just installed the egcs-20001030 snapshot on my Linux box and
>I tried compiling some C++ code using g++ and I'm getting a strange
>error stating that "main must return an int".
A strange error? "void main" has never been correct, as far as I
know, not in C++ and not in C. It is certainly not correct in
standard C++. main()'s return type must be int.
> How can I confirm that this is the case or is this
>just a buglet in the compiler snapshot?
Get hold of a copy of the standard (see URL in sig); it will cost
you only $18.
And, with respect, throw out whatever book you currently use as a
C++ reference. If it tells you to use "void main", it almost
certainly has lots of more subtle errors. The C++ reviews section of
<http://www.accu.org> will suggest some C++ books that are good and
some that are to be avoided.
If you don't have a copy of Stroustrup's /C++ Programming Language/
3rd edition or special edition, you should. It's not always easy to
find answers to questions in that book (my opinion, obviously), but
you can be sure that what is there is correct, and there's no more
comprehensive exposition of the language.
You may also want to download and read the C++ FAQ Lite (URL also in
sig).
--
Stan Brown, Oak Road Systems, Cortland County, New York, USA
http://oakroadsystems.com
C++ FAQ Lite: http://www.parashift.com/c++-faq-lite/
the C++ standard: http://webstore.ansi.org/
reserved C++ identifiers: http://oakroadsystems.com/tech/cppredef.htm
more FAQs: http://oakroadsystems.com/tech/faqget.htm
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]