Writing C/C++ on Windows using g++ under Cygwin

I wrote this ages ago, for the benefit of a Windows user who wanted guidance getting set up with a free C/C++ compiler to learn the language. I recommended using the g++ compiler in a command-line environment under Cygwin, with the following guide to getting started. I recently found it lurking on my hard-drive, and figured it’s not doing any good to anyone sitting there, so here it is, just in case.

Introduction

This article outlines one method of using the open source C/C++ compiler, Gcc to compile C/C++ programs on a Windows PC. It uses Cygwin, which emulates a Linux-like command-line environment, which is many times more useful and powerful than the DOS command line that comes out of the box with Windows.

Of course, if you don’t like all this free-as-in-beer, free-as-in-speech, command-line hippy nonsense, you could always just splash out for Visual Studio instead. Such a well-designed IDE has many strengths – but so does the approach I am about to describe. Choose the tool appropriate to the task in hand.

Install Cygwin

Download and run setup.exe from http://www.cygwin.com/setup.exe

The root directory you specify to the installer, default C:\cygwin, will be your Unix root (/) directory under Cygwin. If this doesn’t mean much to you, see the (currently hyperthetical) section on Unix filesystems below.

Install for all users, and leave default text file type as Unix, not DOS. There are more instructions for Cygwin setup online, but my intention is that this document should be sufficient.

Eventually you get to choose which packages to install. This window is resizable, even though it doesn’t look like it. Maximise it. Also, select the ‘Curr’ radiobutton on the top right – so that we only view the current versions of each package. (as opposed to old or experimental versions.)

Expand the categories on the left by clicking their ‘+’ signs. The ‘current’ column is empty, because you don’t yet have anything installed. Under the ‘new’ column, you can click the word ‘skip’ to select which version of each package you wish to install. This installer understands the dependencies between packages, so selecting package A which requires package B will automatically select package B.

Everything under the ‘Base’ category is already selected, plus their dependencies. This is a minimal Cygwin install, to which we will now add one or two things.

Under the ‘Devel’ section, find the entry for ‘gcc. C compiler upgrade helper.’ Selecting the latest version of this package (3.3.3-2 as of 18 Aug 2004) will automatically select all the other parts of gcc that we need (gcc-core, g++, etc.) as well as the ‘binutils’ package.

Make sure the following packages are selected:

  • Devel/gcc – will automatically select some other packages
  • Devel/make
  • Doc/cygwin-doc

You’ll also need a text editor. You can either select and install one of those listed in the Cygwin installer, or else use any Windows program. Notepad will work fine for the moment. Download Textpad for Windows if you want something a little more useful without being intimidating. Use something like Vi or Emacs if you are already familiar with them.

Click [Next] when you’re done. Setup downloads and installs the selected packages.

Run Cygwin

Double click the Cygwin icon on your desktop. You should get an uninspiring DOS-like text terminal in a window. Type the command ‘ls’, which lists the files in the current directory. From now on I’ll show commands you should type in monospace, preceded by a dollar sign, possibly followed by blue text showing the expected output of the command, like this:

$ ls
My Pictures My eBooks My Music

With any luck, you should recognise the files listed as those residing in your My Documents directory. The yellow tilda (~) above your dollar-sign prompt is a symbol that is used to indicate that you are currently looking at your home directory, which is the Unix way of saying My Documents.

I don’t know how much/little Unix or Linux you know, so I’m going to explain that as little as possible for now, and just press on with what commands you need to type to get a C program compiled.

Create a C file

Create a new directory within ‘My Documents’, where our C programs will live. You can use Linux commands if you know them, or use your Windows Explorer thing. I’m going to use Linux commands:

$ mkdir cprogs
$ cd cprogs

(The ‘cd’ changes directory into the new ‘cprogs’ directory. Note that the yellow ‘~’ above your dollar prompt has changed into a yellow ‘~/cprogs’, to indicate this. So now if we type ‘ls’, then it lists nothing – our new directory is empty.)

Next we want to create our c source file. If you installed vi or emacs as part of the cygwin setup, then you can use these here:

$ vi myprog.c

Otherwise, you can just use notepad, or any other windows text editor. You can invoke it from the command line if you wish.

$ notepad myprog.c

Enter and save the following minimal program:

#include <stdio.h>
 
int main(int argc, char ** argv)
{
    printf ("Hello world\n");
    return 0;
}

Notice that, while notepad is still open, you can no longer type anything in your Cygwin terminal window. Cygwin is waiting for notepad to finish before it lets you type anything else. Save and quit notepad (now you can type again) and then run it once more, appending an ampersand at the end of the command:

$ notepad myprog.c &

The ampersand tells cygwin to run notepad ‘in the background’, while allowing the cygwin terminal to continue accepting input. For example, while notepad is still open, you can now issue a command to compile your program.

Compiling

To compile the program, we invoke the gcc program that we installed as part of cygwin setup:

$ gcc myprog.c -o myprog

This tells the program gcc to compile myprog.c, and to call the output file myprog. If this produces error messages, check the program in Notepad, save again (Ctrl+S in Notepad) and then recompile. Note that you can re-issue the compile command by pressing the up arrow key, then return. If the compile command produces no error messages, nor any other kind of response, then the compilation has been successful. You can verify this:

$ ls myprog.c myprog.exe

(Note: on a real Unix/Linux box, the program would have no ‘.exe’ extension. It would simply be called ‘myprog’.)

And to run your new program:

$ ./myprog
Hello World

Wrapup

That’s the basics. You’ve got yourself a kick-ass C compiler (or C++ compiler if you invoke it with ‘g++’ instead of ‘gcc’) along with a Linux-like environment to use it in. All for free, and without having to repartition your existing Windows install or suffer the frustration of a dual-boot system.

The Art of Unix

It should be noted that a real Unix/Linux development environment will build on the arrangement described above, often in the Unix style of ‘many small tools talking to each other’, rather than as a single monolithic application in the style of Visual Studio. This allows competing tools to be slotted into place, giving the developer the maximum freedom of choice. For example, it is trivial to replace Gcc with the Intel C++ compiler, Icc, resulting in faster execution speed at a cost of cross-platform compatibility. Such flexibility leaves the developer free to retain their favourite text editor, debugger, etc, regardless of the type of work or what project they are working on.

The command-line is often frowned upon by developers more familiar with a GUI way of working, who tend to assume that such an ancient interface represents a cruder way of working. In actual fact, the reverse is true – a GUI is good for providing guidance to novice users, but ultimately proves limited and inflexible. One can only use a GUI application for exactly the uses the original developer envisaged. With a well-designed command-line, the user is faced with a steeper learning curve, but is then free to combine programs in novel and constructive ways.

For example, suppose we had 100 C programs to compile (not so far-fetched if these are actually libraries or plug-ins.) In many IDE GUI environments, the user would have no choice but to spend all afternoon loading each program in turn and clicking the [compile] button. Knowing a little command line magic, however, makes it a ten-second job:

$ for SOURCE in *.c;
> do
> gcc $SOURCE -o ${SOURCE%.c};
> done

Each method has it’s strengths and should be used where they are most appropriate.

20 thoughts on “Writing C/C++ on Windows using g++ under Cygwin

  1. nice job, very useful the beginners of cygwin……. could you tell me how to write tcl script and run it …?? do you have any information about xwinserver ??

    thank you very much for the cygwin explained …..

  2. Ha! Thanks Tony, looks like you were caught out by the same problem I was. Bloody WordPress. Is there a blogging software oriented for programmers rather than real people? Fixed now, anyhow.

  3. Jitesh: Sounds like Cygwin isn’t installed properly, if it can’t even run ‘ls’.

    If I were you, I’d uninstall it completely and install it again.

  4. Dear friend when i launch the cygwin terminal and after that when i write command “ls” i got error
    -sh: fork: retry: Resource temporarily unavailable
    1019070 [main] -sh 4908 child_copy: linked dll data write copy failed, 0x5461E9000..0x5461E9068, done 0, windows pid 8184, Win32 error 998
    1022044 [main] -sh 4908 fork: child 5524 – couldn’t copy linked dll data/bss, errno 11

  5. Shouldn’t first line of the Hello World program be “#include “, not just “#include”?

    Anyway, thanks so much – this was exactly what I was looking for! While I typically use Ubuntu to run my C programs, occasionally I use Windows and would like to test something, and this is the simplest way I’ve found to do it.

  6. Hey Rahul. If you using Cygwin on a Windows machine, then you might have to add the directory of notepad.exe (or the text editor of your choice) to your PATH environment variable. I’m on Windows 7 right now, and typing:

    where notepad.exe

    shows me that it’s in C:\Windows\System32

    It seems unlikely that this dir is not already on your PATH. Have a look, using something like:

    echo %PATH%

    If it isn’t present, then add it:

    set PATH=%PATH%;C:\Windows\System32

    Then try the command again.

    Alternatively, if you are actually on a Linux/Mac/other, then you won’t have notepad.exe available. Find another text editor that is present on your system.

  7. When i type notepad myprog.c in the terminal it shows
    -bash:notepad:command not found

  8. A very good article which is always in my bookmarks to help someone new to CYGWIN

  9. thank you for your help.your article is very useful to new person who don’t know cygwin

  10. It was 10 years back that I ran C with the Turbo C tool…I wanted to get back and downloaded stuff but it was your article which helped me execute(that’s all which matters)…thanks.

  11. Hey Shashi,
    Yes, it works with C++. You have to be sure to select the ‘g++’ compiler during your Cygwin install, and then compile your programs using ‘g++’ instead of ‘gcc’.

  12. Thanks alot for this article. I learnt how to run C program in Cygwin emulator.
    is this emulator having C/C++ editor?
    Thanks
    Shashi Kant

  13. Thanks Michael. If anyone else is ever requires similar help I might try recasting the above exercise with a MinGW slant, see if there’s any mileage in that.

  14. Good points, Josh and Max. I figured it was OK for my friend, who only had access to Windows machines, and just wanted something which which he could try out simply C++ programs for his own benefit, but the considerations you raise are very pertinent in almost any other circumstance.

  15. People using cygwin should be aware that, by default, their executables are linked with GPL cygwin libraries, meaning their code must now be GPL. Add -mno-cygwin to gcc/g++ to avoid this. Even still, the app might not work without the cygwin runtime libs.

  16. Just don’t. If Cygwin seems like a good idea, move to Unix. Because unless they’ve figured out how binary dependencies are supposed to work, it’s just not worth it.

Leave a Reply