Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: alx on November 26, 2003, 10:51:24 AM

Title: G++ problems
Post by: alx on November 26, 2003, 10:51:24 AM
I'm just starting to learn C++, and I've downloaded GCC from Aminet.  I've installed everything as it says and if I do a standard "Hello World" program in C and go:

Work:>GCC -o helloc hello.c

Work:>helloc

It runs nicely.  Now if i write a C++ program:

Quote

#include

int main ()
{
  std::cout << "Hello C++!\n";
  return 0;
}


And go:

Work:>G++ -o hellocpp hello.cpp

Work:>hellocpp

It cannot find "hellocpp" - the compiler hasn't actually written an executable.  If I change the sourcecode a bit:

Quote

#include

int main ()
{
  std::cout << "Hello C++!\n";
  return 0;
}


The compiler compains that it cannot find the header, so I guess it's doing something.  If i do something like this though:

Quote

#include

inain ()

  stdanythjng::cut << "Hello C++!\n" let's not use semicolons
   gerbil
}}}}}


It doesn't actually give any errors, so I suspect the C++ compiler (not the preprocessor) is at fault.  Any ideas?  I'm a C/C++ newbie, and it's running under WinUAE (AIAB)
Title: Re: G++ problems
Post by: Eniodis on November 26, 2003, 11:07:58 AM
Try your first C++ program but...

#include // without .h !

Does it work ?
Title: Re: G++ problems
Post by: alx on November 26, 2003, 12:05:07 PM
Quote
Try your first C++ program but...

#include // without .h !

Does it work ?


No - it complains it cannot find the header files (they all end in .h - even for C++)

---edit---

I'm not in front of my computer now BTW so I cannot check for certain, but that's what I remember...
Title: Re: G++ problems
Post by: Ratto on November 26, 2003, 12:27:27 PM
I've used g++ under amigaos, whitout problem, try this:

#include

using namespace std;

int main(){

cout << " hello" << endl;

return 0;
}

Then save whit the name: hello.cc

Open a shell and write:

g++ hello.cc -o hello

then run hello

This must work  :-D
Title: Re: G++ problems
Post by: bloodline on November 26, 2003, 12:38:40 PM
Quote

#include

int main ()
{
std::cout << "Hello C++!\n";
return 0;
}




#include

Using namespace std;

int main ()
              {
           cout << "Hello C++!" << endl;
      return 0;
}

Title: Re: G++ problems
Post by: bytecode on November 26, 2003, 03:28:27 PM
Just so that you know, standard C++ headers don't have any .h extension. Those are deprecated, pre-standard headers. There is no guarantee that they'll behave the same way on different implementations, or that they'll be present at all.

So, include , , , etc, and not , , , etc.

The same holds true for C headers. There is no guarantee that a complete C89 environment will be present. So include , , etc, and not , , etc.

Everything inside the aforementioned headers should be in the std namespace. So you can use something like:

std::cout << "hello world!" << std::endl;

or

using namespace std;
cout << "hello world!" << endl;

or

using std::cout;
using std::endl;
cout << "hello world!" << endl;

or even:

namespace s = std;
s::cout << "hello world!" << s::endl;


Remember that even C functions should now be in the std namespace (it's sadly not always implemented that way), so if you want to use say, printf, use std::printf (or one of the options above).


Hope this helps.
Title: Re: G++ problems
Post by: Karlos on November 26, 2003, 05:28:05 PM
I think for the majority of beginners, "using namespace std;" is the least hassle. There really isnt much n the standard C library or the C++ STL where you'd ever get indentifier overlaps requiring the selection of a particular member of a given namespace...

That only really tnds to happen in large projects involving many people who always end up inventing the same names for stuff, or in various 3rd party libraries which offer replacement functionality for elements of the standard libraries.
Title: Re: G++ problems
Post by: alx on November 26, 2003, 05:32:00 PM
@Ratto/bloodline

Thanks, but it still doesn't work :-(   When I run "hello", it cannot find it (Hello: Unknown Command).  I get the distinct impression it isn't actually compiling the code - surely:

Quote
#include
supercalifragilisticecpialidosios


Should give an error?  Could I have made a mistake installing the C++ parts?

@bytecode

In the C++ tutorial book I'm using, it mentions that about the .h, and it compiles fine under G++ in windoze.  But the Amiga version I have doesn't seem to have those versions, unless I'm looking at this the completely wrong way (it complains it cannot find them without the .h extension)

BTW Welcome to A.org :-D
Title: Re: G++ problems
Post by: Kronos on November 26, 2003, 06:09:31 PM
Nothing to see here  :-D
Title: Re: G++ problems
Post by: Karlos on November 26, 2003, 06:21:54 PM
@alx

#include is definately erronious :-)

C++ formatted IO streams are cack for console output anyway. Coming from me, Mr. "C++ owns all progrmming languages", thats quite a statement!

I mean theyr'e great for quick and dirty use, but in all honesty, I find the old C printf() a lot more useful, but as with all things C, you have no protection from misusing it...
Title: Re: G++ problems
Post by: alx on November 26, 2003, 06:30:36 PM
Quote
#include is definately erronious


Whoops :-D

What I meant is that, as long as I get the #include bits right, i can write anything into the code and it won't complain - wierd... :-?

---edit---

BTW I remember hearing a while ago that geek gadgets was easier to setup than the individual files from Aminet, but the links on GG seem to be broken, I'd had problems with it before, and I can make GCC compile C progs ATM - just not C++ :-(
Title: Re: G++ problems
Post by: Karlos on November 26, 2003, 06:36:48 PM
I just downloaded that big fat 85Mb GoldEd trial. It comes with gcc 2.95.3 and 3.3 (plus vbcc and other goodies).

I haven't installed it yet, but I hear it does most of the set up for you (the main reason I downloaded the bugger - 85Mb over 56k....)

I always find manual installation of gcc a total nightmare. Guess I am simply not a l33t haXX0r.

Still, on the bright side, I am not a l33t haXX0r, thus implying I have real friends, good lady and a life away from my unix box :lol:
Title: Re: G++ problems
Post by: alx on November 26, 2003, 06:40:24 PM
@Karlos

That sounds worth it, if it comes with an IDE.  Will the trial let me compile without restrictions, or does it time out or something?
Title: Re: G++ problems
Post by: Kronos on November 26, 2003, 06:48:36 PM
Probraly restricted to 1000 lines in the editor (per file), but fully working
on the compiler.
Title: Re: G++ problems
Post by: Karlos on November 26, 2003, 07:15:29 PM
@alx

Yeah, Kronos is right - it has a restriction on the source file length, but the compilers are complete.

Personally I don't mind StormEd for source editing - I got pretty used to it so any restriction in GoldEd's editor doesnt really matter to me.
Title: Re: G++ problems
Post by: Dietmar on November 26, 2003, 08:05:01 PM
>Yeah, Kronos is right - it has a restriction on the source file length, but the compilers are complete.

Compilers are - of course - complete, those are "bonus material" and unrelated to GoldED.  Their installations will be done to a different folder, the devkits tree. You can install the GoldED trial (make sure to install it with C/C++ to get the compilers) and then delete the trial but keep the devkits. I'm not aware of any easier way to install gcc and vbcc on your Amiga (it will deal with such issues as making inlines or install gcc so that it does work with WinUAE and the Windows file system in the absence of links, and it will not require more than a few mouse clicks, promised :). Btw, forget about the gcc on Aminet, it's an obsolete old version.

If you want to download GoldED with devkits and the easy-installation compiler package, make sure that you download the "fat" version (85 MB). It includes three variants of gcc and the free and excellent vbcc compiler. I apologize about the size, it was never meant for downloading.

http://golded.dietmar-eilert.de/golded/files.htm (http://golded.dietmar-eilert.de/golded/files.htm)

As to restrictions, the trial version of GoldED has a restriction of 500 lines per file - that's why it's called a trial.  I believe that is enough to test it or even use its HTML mode as free HTML editor.
Title: Re: G++ problems
Post by: bytecode on November 27, 2003, 02:05:21 AM
Quote
@bytecode
In the C++ tutorial book I'm using, it mentions that about the .h, and it compiles fine under G++ in windoze. But the Amiga version I have doesn't seem to have those versions, unless I'm looking at this the completely wrong way (it complains it cannot find them without the .h extension)


I'm wondering... Have you tried to search for say, "iostream" or "iostream.h"? You might have to set up some environment variable that points to the directory holding the headers.

I remember using gcc on UAE, and prior to building anything, I would run a *nix shell (sh). Try this, maybe?

WORK> sh
> g++ -o hellocpp hello.cpp
> ./hellocpp

It's quite possible that the shell will set the correct variables for you (on *nix I'd tell you to look in $HOME/.bashrc or /etc/bashrc, or even /etc/profile, but I have no idea where this shell emulation might have written it, if at all). Note that *nix is case sensitive, and so will that shell emulator.

If that doesn't work, I'll try and install GCC on UAE again.


Quote

BTW Welcome to A.org


This is actually my 4th or 5th post, but I've been so active that my account disappeared :-D. Thanks though  :-).


Hope this helps.