Can't get 'Hello World' to compile

Hi, I am a COMPLETE newby at programming but am trying hard to get as far as I can on my own. I have installed Cygwin, with the gcc compiler package, and it all seems to be working OK. I'm using the 'Hello World' sample program used in the tutorial here - code is as follows:

// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}

I have saved the text to file c:\cygwin\hello.c, then from within Cygwin I have typed:
gcc /hello.c -o hello.exe. I get the following error messages:

/hello.c:3:20 iostream: No such file or directory
/hello.c:4: error: parse error before "namespace"
/hello.c:4: warning: data definition has no type or storage class
/hello.c: In function 'main':
/hello.c:8: error: 'cout' undeclared (first use in this function)
/hello.c:8: error (Each undeclared identifier is reported only once
/hello.c:8: error for each function it appears in.)

After searching for some answers with Google, I found several others having similar problems. The suggested answers for them did not work for me. One suggested solution was that the iostream library was not installed. I installed the ENTIRE 800 mb Cygwin package, including all libraries. Another suggestion was that it was a pathing issue. I do think I have a pathing issue, or I wouldn't have to use the leading '/' before hello.c - i should be able to use:
gcc hello.c -o hello.exe. But I don't know what else to do about the pathing - I added C:\Cygwin, and C:\Cygwin\home\username\ to my set path environment variable and rebooted Win XP Pro SP3.

The reason I said Cygwin seems to be working OK is that this other program works correctly (taken from Cygwin's User's guide):

http://cygwin.com/cygwin-ug-net/setup-maxmem.html

main()
{
unsigned int bit=0x40000000, sum=0;
char *x;
while (bit > 4096)
{
x = malloc(bit);
if (x)
sum += bit;
bit >>= 1;
}
printf("%08x bytes (%.1fMb)\n", sum, sum/1024.0/1024.0);
return 0;
}

It both compiles and executes properly.

If someone could help straighten me out on this, I'd love to move past step one in learning how to program!

Thank you, Ken
Last edited on
Ah, I do notice that you said you saved the file as c:\cygwin\hello.c.
As this is a C++ file you are trying to do - try saving it as c:\cygwin\hello.cpp.

It may not be the answer to all your problems - but it is certainly the answer to one of them.
Thank you for the reply. I re-saved the file as c:\cygwin\hello.cpp and tried to compile it again. This time I got an entirely new set of errors (but none of the previous ones):

/cygdrive/c/DOCUME~1/username/LOCALS~1/Temp/ccxVOVgI.o:hello.cpp:(.text+0xd): undefined reference to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/cygdrive/c/DOCUME~1/username/LOCALS~1/Temp/ccxVOVgI.o:hello.cpp:(.text+0x60): undefined reference to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/cygdrive/c/DOCUME~1/username/LOCALS~1/Temp/ccxVOVgI.o:hello.cpp:(.text+0x9f): undefined reference to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/cygdrive/c/DOCUME~1/username/LOCALS~1/Temp/ccxVOVgI.o:hello.cpp:(.text+0xce): undefined reference to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/cygdrive/c/DOCUME~1/username/LOCALS~1/Temp/ccxVOVgI.o:hello.cpp:(.text+0x135): undefined reference to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
/cygdrive/c/DOCUME~1/username/LOCALS~1/Temp/ccxVOVgI.o:hello.cpp:(.text+0x13a): undefined reference to 'std::basic_ostream<char, std::char_traits<char>, std::operator<char> >::size() const'
/cygdrive/c/DOCUME~1/username/LOCALS~1/Temp/ccxVOVgI.o:hello.cpp:(.text+0x163): undefined reference to 'std::ios_base::Init::Init()'
/cygdrive/c/DOCUME~1/username/LOCALS~1/Temp/ccxVOVgI.o:hello.cpp:(.text+0x17e): undefined reference to 'std::ios_base::Init::Init()'
collect2: 1d returned 1 exit status

BTW the other program I referred to in the OP is saved as 'max_memory.c' and it compiles fine in Cygwin. No error messages at all and it produces 'max_memory.exe', which works when launched.

Thanks again, Ken
OK, the problem was I was using the wrong compiler - I needed to use g++ instead of gcc AND change to file name to .cpp.

Yay, I'm out of the starting block!

Ken
Damn, I didn't notice the GCC
If you want to continue using GCC you need to add -lstdc++ when you compile.
Topic archived. No new replies allowed.