使用来自 cygwin 的 g++ C++ 编译器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/602937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the g++ C++ compiler from cygwin
提问by user42155
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:
我正在尝试执行我的第一个“Hello World!” 在 C++ 中。我使用的是 Windows XP,并且安装了 cygwin,其中安装了 g++ C++ 编译器。我写了一个小的 hello-world 程序,并将它保存在 hello.cpp 中。从命令提示符我写:
g++ hello.cpp
g++ hello.cpp
But I get:
但我得到:
'g++' is not recognized as an internal or external command, operable program or batch file.
'g++' 不是内部或外部命令,也不是可运行的程序或批处理文件。
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.
我已经在我的 D:\programs\cygwin 中安装了 cygwin。我在 D:\cpp 中用我的 hello-world 文件创建了另一个目录。我的安装或我的路径似乎不太好,但我不知道是什么。我已经运行了cygwin的exe文件,在安装目录下我有所有需要的文件,我想:bin文件夹,lib,Cygwin.bat等。
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?
我已经读过,如果出现此类错误消息,我应该检查 cygwin1.dll 文件是否已复制到 Windows 的主文件夹中。这是 C:\WINDOWS 目录吗?我看过那里,那里没有这样的文件。我在cygwin的bin文件夹中有cygwin1.dll:D:\programs\cygwin\bin。另外,如何检查命令的搜索路径中是否考虑了 bin 文件夹(D:\programs\cygwin\bin)?
I think that the hello-world program shouldn't contain any syntax errors, I have just copied it. Also, when I write
我认为 hello-world 程序不应该包含任何语法错误,我刚刚复制了它。另外,当我写
g++ -v
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:
下面是 hello-world 程序的代码:
#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)
我已经安装了 g++,现在当我输入 g++ -v 时,我得到:从以下位置读取规范:\d\cygnus\cygwin-b20\H-i586-cygwin32\bin..\lib\gcc-lib\i586-cygwin32\ egcs-2.91.57\specs gcc 版本 egcs-2.91.57 19980901(egcs-1.1 版本)
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:
在 bin 目录中,我有 g++.exe 和 gcc.exe。我不明白为什么我在上面得到“gcc”而不是“g++”。我不知道这有多重要,但是当我尝试在 DOS 命令提示符下编译程序时收到一条错误消息:
g++ hello.cpp
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:
这是我尝试在 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
回答by user42155
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.
正如人们所说,检查路径。不要开始复制 cygwin DLL - 这不是必需的,以后可能会出现问题并且无法解决问题。
And when it comes to setting the Windows PATH variable (and others) I've found this small programto be quite useful and better than squinting at the MS control panel aplet.
当谈到设置 Windows PATH 变量(和其他变量)时,我发现这个小程序非常有用,而且比斜视 MS 控制面板 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 :-)
编辑:似乎 OP 错误地没有安装 g++ - 我们所有人的教训,他们首先回答了这个明显的问题:-)
回答by user42155
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?
啊,我还以为我安装cygwin的时候就安装了g++。所以,在这种情况下,我实际上应该首先安装 g++,不是吗?
(and then reboot)
(然后重启)
回答by Milan Babu?kov
Are you sure you installed g++? G++ is C++ compiler, not C compiler. If you only installed C compiler, you would have command gccbut not command g++
你确定你安装了 g++?G++ 是 C++ 编译器,而不是 C 编译器。如果你只安装了 C 编译器,你会有命令gcc而没有命令g++
回答by sfossen
make sure it's on the path or you're running the bash shell cmd prompt.
确保它在路径上,或者您正在运行 bash shell cmd 提示符。
回答by Francis Beaudet
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.
我假设您在这里使用的是 Windows Shell。如果您从 cygwin Bash shell 调用编译器,则应该为您设置所有路径。
回答by Daniel Earwicker
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/
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.
你安装g++了吗?Cygwin 安装程序的默认设置不会安装它。
回答by Daniel Earwicker
Make sure they c:\cygwin\bin is in the PATH.
确保它们 c:\cygwin\bin 在 PATH 中。
Hope it helps.
希望能帮助到你。
回答by RobS
To get the path in cygwin:
要在 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
下一个命令将告诉我们 cygwin 认为它应该在哪里查找 Windows 中的所有这些文件
mount
The line which includes /usr/bin is the one we need a copy of.
包含 /usr/bin 的行是我们需要复制的行。
回答by Hyman BeNimble
bring up dos shell.
调出dos shell。
cd c:\cygwin\bin
cd c:\cygwin\bin
type c++ or cpp
输入 c++ 或 cpp
or do a directory command do see the contents. Look for cpp or c++.
或执行目录命令查看内容。寻找 cpp 或 c++。
If they aren't there, you haven't downloaded it.
如果它们不存在,则您还没有下载它。
回答by Hyman BeNimble
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.
除了将它添加到路径中(这将解决您的问题),您可能还想下载 make 和 dbg,它们也在开发路径上,但默认情况下未选中。确保选择“保持”作为整体安装选项,这样它就不会重新下载所有内容。