如何通过在 Windows 机器上安装 Cygwin 来使用 gcc?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8107052/
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
How can I use gcc by installing Cygwin on a Windows machine?
提问by Jeegar Patel
i have installed the cygwin
package for my netbeans IDE. I can use that in netBeans project. But now I want to use the gcc compiler from a cmd prompt. How can I do that?
我已经cygwin
为我的 netbeans IDE安装了这个包。我可以在 netBeans 项目中使用它。但现在我想从 cmd 提示符使用 gcc 编译器。我怎样才能做到这一点?
In Linux we open a terminal and type
在 Linux 中,我们打开一个终端并输入
gcc filename.c
and it compiles. Now I want to do the same thing in Windows with Cygwin's gcc. Can I type gcc filename.c
in cmd and it compiles? If so, how?
它编译。现在我想用 Cygwin 的 gcc 在 Windows 中做同样的事情。我可以输入gcc filename.c
cmd 并编译吗?如果是这样,如何?
Edit :
编辑 :
by writing in cmd
通过在 cmd 中写入
gcc --version
I get Access is denied
我得到 Access is denied
Edit 1:In the C: drive I have a folder named Cygwin
that contains Cygwin.bat
.
编辑 1:在 C: 驱动器中,我有一个名为的文件夹Cygwin
,其中包含Cygwin.bat
.
When I run that, a new prompt is opened and inside that when I type gcc filename.c
, it works.
当我运行它时,会打开一个新提示,当我输入时gcc filename.c
,它会在其中工作。
In that .bat
file :
在那个.bat
文件中:
@echo off
C:
chdir C:\cygwin\bin
bash --login -i
I don't understand what that means.
我不明白这是什么意思。
回答by Keith Thompson
I see from your latest comment that it works when you run gcc
from the Cygwin bash shell.
我从您的最新评论中看到,当您gcc
从 Cygwin bash shell运行时它可以工作。
The Access is denied
message was appearing when you tried to run gcc
from the Windows cmd prompt. I don't know why you'd get that particular message.
Access is denied
当您尝试gcc
从 Windows cmd 提示符运行时出现该消息。我不知道你为什么会收到那个特定的消息。
My advice is just to use the bash shell. (It also has a lot of nice features that the Windows command shell lacks.) If that's a good solution for you, feel free to stop reading now.
我的建议只是使用 bash shell。(它还具有许多 Windows 命令外壳所缺乏的好功能。)如果这对您来说是一个不错的解决方案,请随时停止阅读。
But if you really want to use Cygwin tools (such as gcc) from a Windows prompt, you need to update your Windows %PATH%
to include the Cygwin bin directory. As seen from bash, the directory is /usr/bin/
; from Windows, it's going to be something like C:\cygwin\bin
(assuming you installed Cygwin in C:\cygwin
, which is the default).
但是,如果您真的想在 Windows 提示符下使用 Cygwin 工具(例如 gcc),则需要更新您的 Windows%PATH%
以包含 Cygwin bin 目录。从 bash 中可以看出,目录是/usr/bin/
; 在 Windows 中,它将类似于C:\cygwin\bin
(假设您在 中安装了 Cygwin C:\cygwin
,这是默认设置)。
To permanently add C:\cygwin\bin
to your Windows %PATH%
, open System Properties
in the Control Panel, tap the "Environment variables" button, and adjust the value of Path
in "System variables". Once you've done that, newly opened cmd windows should have the new %PATH%
setting. (The user interface for modifying environment variables isn't exactly use-friendly; maybe somebody else can suggest a better way.)
要永久添加C:\cygwin\bin
到您的 Windows %PATH%
,请System Properties
在控制面板中打开,点击“环境变量”按钮,然后调整Path
“系统变量”中的值。完成后,新打开的 cmd 窗口应该具有新%PATH%
设置。(用于修改环境变量的用户界面并不完全易于使用;也许其他人可以提出更好的方法。)
EDIT:
编辑:
The cygwin.bat
batch file changes the current director to C:\cygwin\bin
and then launches the Cywgwin bash shell in a new window. That gives you an environment in which gcc
works by default, since your $PATH
is already set up correctly. The windows command shell and the Cygwin bash shell are quite different environments.
该cygwin.bat
批处理文件改变当前导演C:\cygwin\bin
,然后启动Cywgwin bash shell中的一个新窗口。这为您提供了一个gcc
默认工作的环境,因为您$PATH
已经正确设置。windows 命令 shell 和 Cygwin bash shell 是完全不同的环境。
回答by Otiel
If you installed the gcc package for cygwin, you should be able to do simply gcc filename.c
.
如果您为 cygwin 安装了 gcc 包,您应该可以简单地执行gcc filename.c
.
The setup.exe
file of Cygwin lets you choose any additional package you want to add when installing. If you skipped it, simply rerun the setup.exe
.
setup.exe
Cygwin的文件允许您选择安装时要添加的任何附加包。如果您跳过它,只需重新运行setup.exe
.
Packages can be also found on the Cygwin website.
也可以在Cygwin 网站上找到软件包。
回答by David B.
does your cmd recognizes gcc as an installed application? if so, try this
您的 cmd 是否将 gcc 识别为已安装的应用程序?如果是这样,试试这个
gcc -c filename.c -o filename.o
回答by Henrik
gcc is actually a link (which the Command Prompt does not understand). Use gcc-3 or gcc-4 if you need to use the Command Prompt.
gcc 实际上是一个链接(命令提示符不理解)。如果您需要使用命令提示符,请使用 gcc-3 或 gcc-4。