tags:

views:

6354

answers:

14

I am trying to execute my first "Hello World!" in C++. I am using Windows XP, and I have installed cygwin, in which the g++ C++ compiler is installed. I have written a small hello-world program, and saved it in hello.cpp. From the command prompt I write:

g++ hello.cpp

But I get:

'g++' is not recognized as an internal or external command, operable program or batch file.

I have installed cygwin in my D:\programs\cygwin. I have made another directory with my hello-world file in D:\cpp. Something with my installation or my paths seems to be not Ok, but I cannot figure what. I have run the exe file of cygwin, and in the installation directory I have got all necessary files, I think: bin folder, lib, Cygwin.bat, etc.

I have read that in case of such error message I should check whether the cygwin1.dll file is copied into the main folder of Windows. Is this the C:\WINDOWS directory? I have looked there, and I don't have such a file there. I have cygwin1.dll in the bin folder of cygwin: D:\programs\cygwin\bin. Also, how do I check whether the bin folder (D:\programs\cygwin\bin) is considered in the search path of commands?

I think that the hello-world program shouldn't contain any syntax errors, I have just copied it. Also, when I write

g++ -v

I get the same error message: that the command is not recognized. I would appreciate if someone give me a hint what should I look at. Thank you.

Here is the code of the hello-world program:

#include <iostream>
using namespace std;

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

I have installed g++, and now when I type g++ -v, I get: Reading specs from: \d\cygnus\cygwin-b20\H-i586-cygwin32\bin..\lib\gcc-lib\i586-cygwin32\egcs-2.91.57\specs gcc version egcs-2.91.57 19980901 (egcs-1.1 release)

In the bin directory I have both g++.exe, and gcc.exe. I don't understand why I get "gcc" above and not "g++". I don't know how important this is, but I receive an error message when I try to compile the program in the DOS command prompt:

g++ hello.cpp

I get:

hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)

And here is what I get when I try to compile the program in the cygwin shell:

hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
+1  A: 

Make sure they c:\cygwin\bin is in the PATH.

Hope it helps.

+2  A: 

I am assuming you are using the Windows Shell here. If you invoke the compiler from a cygwin Bash shell, all the paths should be set for you.

Francis Beaudet
A: 

I wrote a tutorial about this a few years ago, which might help:

http://www.codeguru.com/cpp/misc/misc/compilerandpre-compiler/print.php/c8107__1/

Did you install g++? It's not installed by the default settings of the Cygwin installer.

Daniel Earwicker
+2  A: 

As people have said, check the path. Don't start copying the cygwin DLLs around - that is not necessary, may cauxse problems later and won't solve the problem.

And when it comes to setting the Windows PATH variable (and others) I've found this small program to be quite useful and better than squinting at the MS control panel aplet.

Edit: It seems the OP had mistakenly not installed g++ - a lesson for all of us who replied in asking the obvious question first :-)

anon
+1  A: 

To get the path in cygwin:

echo $PATH

This will get us on the road to understanding the problem.

Edit:

The next command will tell us where cygwin thinks it should be looking for all those files within windows

mount

The line which includes /usr/bin is the one we need a copy of.

RobS
How do I copy the result from the shell? It is very long, and I suppose there should be a way, but I simply can't copy anything from the shell. If there is no way, I will try to write it here (it is 10 lines)
I have put D:\programs\cygwin\bin in my Path in Control Panel --> System --> ... --> Environment variables. Is this the way to set the path?
try: echo $PATH > mypathfile.txtThen open mypathfile.txt in wordpad or something to copy it.
RobS
The fact that you had several lines of output indicates that you're running the command from within cygwin which is a good start.
RobS
Thank you, I have saved the output in mypathfile.txt, and pasted it below.
A: 

In addition to adding it to the path (which will fix your problem), you may want to download make and dbg, which are also on the development path but not checked by default. Make sure to select "keep" as the overall install options so it doesn't re-download everything.

Jack BeNimble
+2  A: 

make sure it's on the path or you're running the bash shell cmd prompt.

sfossen
A: 

Hello,

here is the output of typing echo $PATH in the cygwin shell:

/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/c/Program Files/Perl/site/bin:/usr/bin:/cygdrive/c/Program Files/Perl/bin:/cygdrive/c/program files/perl/bin/:/cygdrive/c/program files/ibm/uima/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/system32/wbem:/cygdrive/d/program_files/Java/jdk1.6.0_03/bin:/cygdrive/c/program files/apache-ant/apache-ant-1.7.0/bin:/cygdrive/c/program files/prolog/bin:/cygdrive/d/a_my_docs/workspace/uima_workspace/my_dateannotator/some/typename/you/dateannotator.java:/cygdrive/c/program files/xalan/xalan-j_2_7_0/xalan.jar/my_dateannotator/some/typename/you/dateannotator.class/c:/program files/gate-3.1/bin:/cygdrive/c/program files/gate-3.1/lib:/cygdrive/d/MINIPAR/pdemo:/cygdrive/c/Program Files/Graphviz2.18/Bin

I have really invoked initially g++ hello.cpp from the DOS command prompt, which was wrong. When I type the same within the cygwin shell, I still have a problem, here is the message:

bash: g++: command not found

I have also added D:\programs\cygwin\bin in my Path in Control Panel --> System --> ... --> Environment variables. Probably I should set the path in another way?

Good, now the output of the mount command (as mentioned in my edited answer) would be useful. Once we see that we might know if your path is ok. (g++ lives in /usr/bin in my copy of cygwin which is in your path)
RobS
I suppose the obvious question is "Have you installed g++?" The cygwin installer is a piece of sh*t, so you may well not have done.
anon
Here is the copy output of mount with the /usr/bin information:D:\programs\cygwin\bin on /usr/bin type system (binmode)D:\programs\cygwin\lib on /usr/lib type system (binmode)
Ok, I will install g++ (I thought i is automatically installed when I isntalled cygwin)
A: 

As others have mentioned, it's probably a path issue, so you need to look at the environment variable PATH. To do this, right click on My Computer and click "Properties". Go to the "Advanced" tab and click the "Evironment Variables" button. From here, see if the paths you mentioned are included in the PATH variable. If not, add them using the same syntax you see there to separate paths (I think it's a semi-colon or a colon between paths.)

Edit: Ah, I see you've already done this (beat my post by mere seconds.) I think all you need to do now is reboot.

Scottie T
Because /usr/bin is in gemm's path already I'm not sure rebooting will help, but with windows you never know.... ;)
RobS
+2  A: 

Ah, I have thought that g++ is installed when I install cygwin. So, I should actually first install g++ in that case, shouldn't I?

(and then reboot)

Bingo -- not sure if the reboot is necessary.
JohnMcG
The Cygwin setup gives you a plethora of packages to select from, make sure you selected g++ there.
reinierpost
A: 

bring up dos shell.

cd c:\cygwin\bin

type c++ or cpp

or do a directory command do see the contents. Look for cpp or c++.

If they aren't there, you haven't downloaded it.

Jack BeNimble
A: 

Are you sure you installed g++? G++ is C++ compiler, not C compiler. If you only installed C compiler, you would have command gcc but not command g++

Milan Babuškov
A: 

Hello,

I have installed g++, and now when I type g++ -v, I get: Reading specs from: \d\cygnus\cygwin-b20\H-i586-cygwin32\bin..\lib\gcc-lib\i586-cygwin32\egcs-2.91.57\specs gcc version egcs-2.91.57 19980901 (egcs-1.1 release)

In the bin directory I have both g++.exe, and gcc.exe. I don't understand why I get "gcc" above and not "g++". I don't know how important this is, but I receive an error message when I try to compile the program:

g++ hello.cpp

I get:

hello.cpp:1: parse error before character 0357
hello.cpp: In function 'int main()':
hello.cpp:'cout' undeclared (first use this function)
hello.cpp: (Each undeclared identifier is reported only once
hello.cpp: for each function it appears in.)
hello.cpp: 'endl' undeclared (first use this function)
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files
g++.exe: hello.cpp: No such file or directory
g++.exe: No input files

I do have hello.cpp in the current directory.

try changing the line endings.
sfossen
which editor did you use to create the hello.cpp file?
anon
What do you mean by changing the line endings?
I have used Notepad.
Thank you all for the replies, anyway, it still doesn't work, but I am going to work on this tomorrow.
Why is this posted as an answer? You should edit the original questio and add it. Or you expect everyone to give answers in the comment box?
Milan Babuškov
A: 

Try to create a new file and type in the code yourself without copying and pasting it. You may have an illegal character in your code which may not be visible in your editor of choice.