delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/05/12:32:23

Reply-To: <arfa AT clara DOT net>
From: "Arthur" <arfa AT clara DOT net>
To: "DJGPP Mailing List" <djgpp AT delorie DOT com>, "Jason" <jpsaave AT sandia DOT gov>
Subject: RE: Why doesn't C++ work!?!?!
Date: Sat, 5 Sep 1998 17:31:48 +0100
Message-ID: <000301bdd8ea$ad71b8a0$5d4d08c3@arthur>
MIME-Version: 1.0
Importance: Normal
In-Reply-To: <35F0791F.D8C61007@sandia.gov>

> --------My Code---------------
> #include <iostream.h>
>
> main(void)
> {
> cout << "This is a test.";
> }
> ------------------------------
>
> > It will throw a wobbley if you don't define main() as int main()
>
> The program has worked this way with all other C++ compilers - I was
> taught that the return value of any function defaults to int if not
> specified.

According to the C++ standards, all functions must be prototyped. the main
function has a quasi-prototype returning an integer and accepting either
nothing or the parameters passed to the program. If you don't use int main()
then it will cause a warning to show, if not a fatal error. Turn on -Wall or
something to see what I mean. GCC is pretty tolerent of this sort of thing,
but that doesn't mean that every other compiler is. It is also considered
bad programming practice by many factions, and bad programming (period) by a
small minority.

> > (any function parameters not declared will be counted as void in C++,
>
> I did specify the parameter as being void.  -->  main(void)

You don't need to in C++. int main(void) and int main() are identical in
C++, but not in C. OK, so that one was picky.

> > Also note that at the end of the program you must have a return 0;
>
> I understand this is normal programming practice, but omitting this
> statement has produced comilable programs in the past with every other
> compiler I have used.

See the note above for main returning an integer. Not that if you do use int
main() then you really should place a return 0; statement at the end of your
code.

> > C++ standards state that you should output an endl at the end of each
> > line you stream to the stdout.
>
> Again, this was never required to compile programs with other compilers.

It's not required. If you want to place a carriage return at the end of a
line in C, you use \n. In C++ you can use endl, but it's not necessary. It
does have it's uses.

The "feature" of DJGPP's buffered output still applies, though.

> > gcc will not link with the C++ libraries according to extension. To do
this
> > you must use gxx (which will).
>
> Well, if this is the case, then someone should notify the DJGPP people
> that their info is misleading on the website.  Here is an exerpt:
>
> ------------
> Based on the file extension that you gave your program, it selects the
> appropriate  commands it needs to run to turn the source you gave it
> into the output file you specified. ... Here is a list of file
> extensions and what gcc does with them.
> ...
> .cc .cxx .cpp .c++ .C (upper case)
>     C++ sources. These are preprocessed with cpp and then compiled with
> cc1plus. You need gpp*b.zip.
> ...
> -------------
> -- Taken from
> "http://www.delorie.com/djgpp/doc/ug/basics/compiling.html"

That file doesn't actually state which program is being used, although it
does imply gcc. The GNU manual (and the readme files) specifically describes
using the g++ program (which happens to be gpp or gxx for those on Win/Dos
based machines) when compiling C++ programs. All I can say is that you have
to read all the readmes and the faq thoroughly.

Also, if you had used the Zip Picker at www.delorie.com to download your
files, you would have seen the following information:

----------
Note: To view online documentation and help, run the "info" program.

Note: Sample compile command: gcc hello.c -o hello.exe

Note: Use gxx (or append -lstdc to your gcc command line) to compile and
link C++ programs (*.cc)

Note: Append -lobjc to your gcc command line to link Objective-C programs
----------


James Arthur
jaa AT arfa DOT clara DOT net
ICQ#15054819

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019