Click to See Complete Forum and Search --> : some cpp info?


bobtcowboy
09-11-2001, 12:38 AM
I want to write C++ code that compiles under (hopefully at least) Linux, Win32, MacOSX (in that order of preference) Am I dreaming?

I guess what I'm wondering is this: is there a standard set for what C++ is? How do I avoid non-portable code?

next... will using GCC and whatever else *it* uses (some cpp libraries, I'd imagine?) force my code or any part of it to be GPL? If so is there an alternative like BCC? (heh)

If I want to make GUI C++ apps, am I better off not using a vi + gcc-type combo? what do you all reccomend for something of the scale of say.... an IRC client?

is it possible I don't have something installed correctly? A simple Hello World program I got off a tutorial site wouldn't compile!


#include <iostream.h>

void main(void)
{
cout << "Hello World\n";
}


Of course, in retrospect.. I should have looked at the URL ( http://www.geocities.com/SiliconValley/Bay/1055/lcpart1.htm ) and known better... but for giggles, what *is* wrong with it?

ok... this is long enough for now... more to come later, I'm sure.

Bill

snivlle
09-11-2001, 01:32 AM
Using Borland or VC++ in Windows, your simple example would compile and run just fine. Within Linux, your function ( main is a function. .the main one actually. .*smile*)needs to "return something" Gcc doesn't understand "void function(void)" try this instead:

#include <iostream.h>

int main()
{
cout << "Hello World\n";
return 0; //that's a zero, not an o.
}

[ 11 September 2001: Message edited by: snivlle ]

The Kooman
09-11-2001, 01:35 AM
<STRONG>I want to write C++ code that compiles under (hopefully at least) Linux, Win32, MacOSX (in that order of preference) Am I dreaming?</STRONG>

Well, I havn't really worked on anything of those platforms other than Linux, but I don't see why "Standard C++" shouldn't compile on any of the systems (if they claim to support standard C++). Problems come when you use non-standard features/extensions/libraries. E.g. try getting MFC to Linux ;) heh, heh!

<STRONG>I guess what I'm wondering is this: is there a standard set for what C++ is? How do I avoid non-portable code?</STRONG>

In the worst case, use macros to seperate your code:

#ifdef LINUX
/* linux specific stuff here */
// or should it be using '//' since we're talking c++ ;)
#elif defined WINDOZE
// blah
#elif defined MAC
// blah
#endif


<STRONG> next... will using GCC and whatever else *it* uses (some cpp libraries, I'd imagine?) force my code or any part of it to be GPL?</STRONG>

What is the "whatever else"?? Using gcc won't. Using bison won't. Using flex won't. You can develop commercial, closed-source, applications using these tools. Check out the gpl faq (at http://www.gnu.org) for more.

<STRONG>If I want to make GUI C++ apps, am I better off not using a vi + gcc-type combo?</STRONG>

Whats wrong with vi(m) + gcc?? I can't think of anything more powerful (except, perhaps, emacs + gcc ... but I risk starting a flame war here!! Read my signature : flames &&gt; /dev/null :D) I use vim for everything now-a-days - typing mails, typing this reply, typing code, drawing ascii art - and I won't dream of using anything else (except perhaps emacs :D)

<STRONG>
is it possible I don't have something installed correctly? A simple Hello World program I got off a tutorial site wouldn't compile!
</STRONG>

The code looks fine. What was the error message you got? How did you invoke the compilation? What are the steps you followed? No offence if you already know all this, but since you're tinkering with a "hello world" level program, I assume you're a newbie!

bobtcowboy
09-11-2001, 02:32 AM
Originally posted by The Kooman:
<STRONG>
The code looks fine. What was the error message you got? How did you invoke the compilation? What are the steps you followed? No offence if you already know all this, but since you're tinkering with a "hello world" level program, I assume you're a newbie!</STRONG>

no offense taken! I am a newbie to c++ :)

I used this code from the comment above


#include &lt;iostream.h&gt;

int main()
{
cout &lt;&lt; "Hello World\n";
return 0; //that's a zero, not an o.
}


and I get this:


/tmp/ccYDcMOO.o: In function `main':
/tmp/ccYDcMOO.o(.text+0xf): undefined reference to `cout'
/tmp/ccYDcMOO.o(.text+0x14): undefined reference to `ostream: :operator&lt;&lt;(char const *)'


after I run "gcc hi.cpp" ... I have version 2.95 of gcc (running progeny debian)

which makes me think that I have some library installed wrong...?

oh... and the "whatever else" I guess is like the libraries? I dunno... I'm a total newbie to gcc.... I'm used to using java + javac ....

thanks for helpin me!

Bill

augur
09-11-2001, 06:14 AM
Greetings,

Try compiling it with: "g++" instead of "gcc".

The Kooman
09-11-2001, 06:25 AM
Originally posted by bobtcowboy:
<STRONG>
#include &lt;iostream.h&gt;

int main()
{
cout &lt;&lt; "Hello World\n";
return 0; //that's a zero, not an o.
}


and I get this:


/tmp/ccYDcMOO.o: In function `main':
/tmp/ccYDcMOO.o(.text+0xf): undefined reference to `cout'
/tmp/ccYDcMOO.o(.text+0x14): undefined reference to `ostream: :operator&lt;&lt;(char const *)'


after I run "gcc hi.cpp" ... I have version 2.95 of gcc (running progeny debian)

which makes me think that I have some library installed wrong...?

oh... and the "whatever else" I guess is like the libraries? I dunno... I'm a total newbie to gcc.... I'm used to using java + javac ....

thanks for helpin me!

Bill</STRONG>

Yes, use g++ instead. Just for "kicks", try renaming the file to something like hello.CC (2 capital 'C's) and then try gcc! I know it sounds crazy, but I think gcc tries to infer the language by looking at the extension. I hope its not taking the cpp extension to be a "c preprocessed file" extension! ... Just a wild guess!

bobtcowboy
09-11-2001, 10:43 PM
Originally posted by The Kooman:
<STRONG>
Yes, use g++ instead.
</STRONG>

Note:
I decided to try to keep myself busy....

Hey... whatta ya know? it worked *sigh... so I'm slightly retarded... trying it as a CC file didn't though

Does anyone have a good C++ tutorial handy?

Bill

The Kooman
09-11-2001, 11:50 PM
Originally posted by bobtcowboy:
<STRONG>
Does anyone have a good C++ tutorial handy?

Bill</STRONG>

Here's a collection of C++ related links I had a long time ago (almost a year ago)! I don't know how many of them are still valid but here they are anyway!
http://www.icce.rug.nl/docs/cplusplus http://www.cerfnet.com/~mpcline/c++-faq-lite/ http://cmwww.unn.ac.uk/~adrian/c++/cpppaper http://www.desy.de/user/projects/C++/courses/cc/Tutorial http://www.swcp.com/~dodrill/cppdoc/cpplist.htm http://www.itr.ch/courses/case/BoochReference/ http://www.icce.rug.nl/docs/cplusplus/cplusplus.html http://www.eng.uc.edu/~jtilley/tutorial.html http://www.cs.buffalo.edu/~milun/unix.programming.html

klamath
09-12-2001, 02:15 AM
Within Linux, your function ... needs to "return something" Gcc doesn't understand "void function(void)".

That's (totally) wrong; of course GCC allows a function to return void.

BTW, it's bad form to declare the arguments to a function in C++ as "foo(void)" -- it should be "foo()" if you really mean "this function takes no arguments".

If you're doing C++ development, you might want to try using GCC3.

For the code itself, ANSI C++ (i.e. the official standard) should be portable. Just make sure that the libraries you use are portable across the platforms you're interested in. For some things (e.g. threading), this often means using a library like the "Common C++ Library" (@ gnu.org) which provides a cross-platform API for that stuff. Also, if you're writing a GUI app, the same applies to your GUI toolkit -- but QT is very portable, so you'll probably be okay. You'll also need to consider platform differences, like line endings and path names.

The Kooman
09-12-2001, 07:58 AM
Originally posted by klamath:
<STRONG>[CODE]BTW, it's bad form to declare the arguments to a function in C++ as "foo(void)" -- it should be "foo()" if you really mean "this function takes no arguments".</STRONG>

I'd say the opposite is true! Its bad to declare a function taking no arguments as "foo()". The empty-parenthesis way of declaration was the "old" C way of declaring functions and it means that foo is a function taking variable number of arguments and returning int. Try it :). If you use the -Wall argument to gcc, you'll see the warnings! The correct way of doing it is "void foo(void)". Thats because the compiler can now do a proper type-check of the calls to "foo".

pinoy
09-12-2001, 07:13 PM
Standard C and C++ defines that main() *must* return an int. There are only a few valid definitions of main and they are:

int main();
main();
int main(void);
main(void);
int main(int argc, char *argv[]);
int main(int argc, char **argv);
int main(int argc, char *argv[], char *envp[]);
etc.


void main(void) (whatever they say) is illegal. It's just that most compilers tolerate this.

klamath
09-12-2001, 09:07 PM
I'd say the opposite is true! Its bad to declare a function taking no arguments as "foo()". The empty-parenthesis way of declaration was the "old" C way of declaring functions and it means that foo is a function taking variable number of arguments and returning int. Try it . If you use the -Wall argument to gcc, you'll see the warnings! The correct way of doing it is "void foo(void)". Thats because the compiler can now do a proper type-check of the calls to "foo".

Right -- if this was C code, I'd agree with you. However, the C++ standard doesn't allow "foo()" to mean "function foo, accepts any arguments". To get this effect in C++, you need to use "foo(...)". Thus, in C++ it is bad form to use "foo(void)", since this is just the unusual C syntax for saying "foo()".

Standard C and C++ defines that main() *must* return an int

You're completely right, of course. All I was saying is: (a) a function is allowed to return void, whether in Linux or Windows (someone said 'In Linux a function needs to return something -- it doesn't understand "void function(void)" '). (b) the "main" function is _allowed_ to return void -- but as you said, this is technically incorrect.

The Kooman
09-12-2001, 11:47 PM
Originally posted by klamath:
<STRONG>However, the C++ standard doesn't allow "foo()" to mean "function foo, accepts any arguments". To get this effect in C++, you need to use "foo(...)".</STRONG>

Thats right. IIRC, even ANSI C would require the ellipsis.

<STRONG>Thus, in C++ it is bad form to use "foo(void)", since this is just the unusual C syntax for saying "foo()".</STRONG>

I don't think so! When you're saying "foo(void)" you're telling the compiler, "Old C or not, this function takes no parameters!" By saying "void foo(void)" there can be no mistake about it being interpreted the "old C way". In fact, in "man g++",
-fno-strict-prototype
Consider the declaration int foo ();. In C++, this means that the function
foo takes no arguments. In ANSI C, this is declared int foo(void);. With
the flag `-fno-strict-prototype', declaring functions with no arguments is
equivalent to declaring its argument list to be untyped, i.e., int foo ();
is equivalent to saying int foo (...);.


So you have to explicitly ask the compiler to interpret it in the "old C' style (of course, this is only g++). But by putting an explicit "void" in the parmeter list you're saying, "No parameters - C style or not!"

Of course, thats only my opinion :D!

Rats, too many "of course"s crept in!

sans-hubris
09-13-2001, 12:58 PM
Since you're new, just learn console programming for now. GUI programming can be extremely complex.

However, once you've gotten console programming down, you can check out Troll Tech (http://www.trolltech.com) and WxWindows (http://www.wxwindows.org).