简体   繁体   English

如何在Windows计算机上安装Cygwin来使用gcc?

[英]How can I use gcc by installing Cygwin on a Windows machine?

i have installed the cygwin package for my netbeans IDE. 我已经为我的netbeans IDE安装了cygwin软件包。 I can use that in netBeans project. 我可以在netBeans项目中使用它。 But now I want to use the gcc compiler from a cmd prompt. 但是现在我想从cmd提示符下使用gcc编译器。 How can I do that? 我怎样才能做到这一点?

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. 现在,我想在Windows中使用Cygwin的gcc做同样的事情。 Can I type gcc filename.c in cmd and it compiles? 我可以在cmd中键入gcc filename.c并编译吗? If so, how? 如果是这样,怎么办?

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. 我不明白那是什么意思。

I see from your latest comment that it works when you run gcc from the Cygwin bash shell. 我从您的最新评论中看到,当您从Cygwin bash shell运行gcc时,它可以工作。

The Access is denied message was appearing when you tried to run gcc from the Windows cmd prompt. 当您尝试从Windows cmd提示符运行gcc时,出现“ Access is denied消息。 I don't know why you'd get that particular message. 我不知道您为什么会收到该特定消息。

My advice is just to use the bash shell. 我的建议只是使用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. (它还具有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. 但是,如果您确实想从Windows提示符下使用Cygwin工具(例如gcc),则需要更新Windows %PATH%以包括Cygwin bin目录。 As seen from bash, the directory is /usr/bin/ ; 从bash可以看出,该目录为/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,它将类似于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". 要将C:\\cygwin\\bin永久添加到Windows %PATH% ,请在“控制面板”中打开“ System Properties ”,点击“环境变量”按钮,然后在“系统变量”中调整Path的值。 Once you've done that, newly opened cmd windows should have the new %PATH% setting. 完成此操作后,新打开的cmd窗口应具有新的%PATH%设置。 (The user interface for modifying environment variables isn't exactly use-friendly; maybe somebody else can suggest a better way.) (用于修改环境变量的用户界面并非完全易于使用;也许其他人可以提出更好的方法。)

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. cygwin.bat批处理文件将当前cygwin.bat更改为C:\\cygwin\\bin ,然后在新窗口中启动Cywgwin bash shell。 That gives you an environment in which gcc works by default, since your $PATH is already set up correctly. 这为您提供了一个默认情况下可以在其中运行gcc的环境,因为您的$PATH已正确设置。 The windows command shell and the Cygwin bash shell are quite different environments. Windows命令外壳和Cygwin bash外壳是完全不同的环境。

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. Cygwin的setup.exe文件使您可以选择要在安装时添加的任何其他软件包。 If you skipped it, simply rerun the setup.exe . 如果您跳过它,只需重新运行setup.exe

Packages can be also found on the Cygwin website . 软件包也可以在Cygwin网站上找到。

does your cmd recognizes gcc as an installed application? 您的cmd是否将gcc识别为已安装的应用程序? if so, try this 如果是这样,请尝试

gcc -c filename.c -o filename.o

gcc is actually a link (which the Command Prompt does not understand). gcc实际上是一个链接(命令提示符不理解)。 Use gcc-3 or gcc-4 if you need to use the Command Prompt. 如果需要使用命令提示符,请使用gcc-3或gcc-4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM