www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/17/01:58:24

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199606170547.AA267780439@relay1.geis.com>
Date: Mon, 17 Jun 96 05:16:00 UTC 0000
To: qliang AT ix DOT netcom DOT com
Cc: djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: Help Beginning

Reply to message 3085594    from QLIANG AT IX DOT NET on 06/16/96  9:55PM


There are a couple of things I noticed right off the bat:

>//myfile.cc
>#include <iostream.h>
>void main()

DO NOT USE VOID MAIN()!!!  This is just plain BAD programming.
For the last time, it's INT MAIN()!  The return value of main() is the
value your program returns to the operating system, and you really
need to specify this, regardless of anything else you plan to do.
BTW, return 0; is the standard way to tell DOS (et.al.) that your
program ran ok.

Sorry for the steam, but we've had this discussion here before.  :)
Question for you:  Where'd you learn to use 'void main()'?  From
a book or a teacher?  Whichever it is, I'd recommend that you
slow roast it/him/her.  :)

>{
>int a=3D10;
>double d=3D10;

Are you sure that you meant "D" here?  Last I checked, thought the
correct exponent char was "E".  I could be out of touch, though...
Or is that just one of those funky pseudocharacters that some mailers
& newsreaders seem to insert randomly all over the place?

>out<<"test\n";
>out<<a<<" "<<d;

This should be "cout", not "out".  Another sign of a good programmer
is the ability to proofread one's own code.

>return 0;

This ought to have given you an error about a void function returning
a value.  Be sure to compile with -Wall - it's an invaluable tool for catching
minor errors.

>gcc -v -o mf myfile.cc

Several things to point out here:

1)  Make sure you have the distribution of bnu252b.zip dated Feb 22, because
it contains a program called "gxx", which really helps in compiling C++
programs.  (It's the DOS translation of "g++", which is an illegal filename
in DOS.)

2)  You should be using "gxx" to compile C++ programs.  :)  Either that
or link libgpp.a with the command "-lgpp" attached to the end of your
command line.

3)  As a novice programmer (or even an experienced one), you should
add three things to your command line besides those mentioned above.

    a)  -Wall to give warnings about all sorts of potential bugs in your
program,
        like invalid typecasts, returning values in functions declared void,
bad
        calls to functions like printf(), etc.

    b)  -O to turn on basic optimization routines.  This not only makes your
        code smaller and faster, but enables -Wall to catch more bugs.

    c)  -g to include debugging information in your executable.  When you
        begin using debuggers like gdb, fsdb, edebug32, and even the useful
        symify, you will appreciate the value of this switch.

So your command line should really look like this:

gxx -Wall -g -O [-v] -o mf myfile.cc     /* you don't have to use -v unless
                                           you like screens full of garbage :)
*/

You can also use:

gcc -Wall -g -O [-v] -o mf myfile.cc -lgpp

But this can cause other minor problems that all vanish if you get gxx.

In your second program:

>void my::show()
>{
>cout<<a<<"\n";

Here you've got cout right, but read on...

>main()

Don't declare main like this either.  Sure, this makes it default to int,
but why make yourself a slave to the compiler?  Declare it int
explicitly to avoid the ambiguity.

>myfile.cc(.text+0x1d): undefined reference to `cout'
>myfile.cc(.text+0x22): undefined reference to `ostream::operator<<(char =
>const *)'
>myfile.cc(.text+0x4d): undefined reference to `cout'
>myfile.cc(.text+0x52): undefined reference to `ostream::operator<<(int)'
>myfile.cc(.text+0x5d): undefined reference to `ostream::operator<<(char =
>const *'

This is all because you didn't tell gcc to include libgpp.a, which defines
all the basic streams & classes.  See above for how to do this, but you're
probably better off just using "gxx" to compile.

One other thing... if you plan to distribute your C++ code, you'd better
examine the GNU General Public License (which is included with DJGPP)
because libgpp.a falls under this.  If you don't want to have to distribute
source or object codewith your programs, you can try just using
libiostream.a instead of libgpp.a, which defines just the standard streams,
but not most of the classes.

Finally, most of this information is in the Frequently Asked Questions list,
which you really ought to obtain and read; it can be found as faq200b.zip
at the same place that you got DJGPP.

John

- Raw text -


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