Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

In Linux we open a terminal and type

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?

Edit :

by writing in cmd

gcc --version

I get Access is denied

Edit 1: In the C: drive I have a folder named Cygwin that contains Cygwin.bat.

When I run that, a new prompt is opened and inside that when I type gcc filename.c, it works.

In that .bat file :

@echo off

C:
chdir C:\cygwin\bin

bash --login -i

I don't understand what that means.

like image 825
Jeegar Patel Avatar asked Sep 01 '25 16:09

Jeegar Patel


1 Answers

I see from your latest comment that it works when you run gcc from the 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.

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.

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).

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.)

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.

like image 198
Keith Thompson Avatar answered Sep 04 '25 07:09

Keith Thompson