The following explains how to set up your machine using free open-source software so that you can write and compile C code. I'm going to show you how to do this with the MinGW compiler and the Eclipse IDE. (There are of course many other possible options: Cygwin or DJGPP for your compiler, or a different IDE.)
MinGW is short of "Minimalist GNU for Windows". Downloading and installing it can be a little tricky, though. Go to the Downloads page. Select the Installer directory and then mingw-get-inst. Find the most recent version there and download the corresponding .exe. Run this file.
If you change the default install directory, make sure the full path does not have any spaces in it. (Spaces in the name can cause you trouble later.)
You should install:
make
command later.)
mingw-get-inst will then open a console window to perform all the downloading and installing.
MSYS will give you a shell you can work in, but this is very inconvenient. You should fix your PATH settings to include the MinGW\bin and the MSYS\1.0\bin directories. Instructions are here.
Open a new Windows Command Prompt. Ask for the --version
of gcc, g++, gdb, and make. You should get a valid version message for each one. The exact numbers don't really matter, but it should look sort of like this:
C:\Files\workspace>gcc --version gcc (GCC) 4.6.2 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. C:\Files\workspace>gdb --version GNU gdb (GDB) 7.3.1 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "mingw32". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. C:\Files\workspace>g++ --version g++ (GCC) 4.6.2 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. C:\Files\workspace>make --version GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for i686-pc-msys
If you get a message like this instead:
'gcc' is not recognized as an internal or external command, operable program or batch file.
then either your PATH variable is not set correctly or (less likely) something went wrong during the install.
At this point, you can now use a simple text editor (such as Notepad) to write your .c code and then use the normal unix commands on the Windows Command Prompt to compile your code. For example, if you have a program named hello.c, open a prompt and use the cd
command to move into the same directory as where your hello.c file is save. To compile it:
gcc -o hello.exe hello.c
And then to run it:
hello
These instructions will be fairly brief. If you need more help, do a Google search or bring your laptop in to lab or office hours.
The CDT plugin lets you write C/C++ code using Eclipse. However, it does not come with its own compiler, so you'll need to follow the directions above to install MinGW (and Msys, if you want make
). Once that's working, you can move on to getting the IDE working.
First, download and install Eclipse.
If you don't have Eclipse already installed, you might consider trying the new "Eclipse IDE for C/C++ Developers", which already includes the CDT plugin.
If you already have Eclipse installed (or if you want a different base Eclipse package), you can install the CDT plugin separately. The easiest way to do this is to download it directly from within in Eclipse itself. The instructions on how to do this are here. In short, you'll go to Help -> Install New Software..., paste in the URL to the code repository, and then select and install the C/C++ Development Tools. (That's the only package you should need.)
Due to how CDT is implemented, the console window does not act like a proper console on Windows machines. Instead, the output stream is buffered. This means output is generally not displayed until the buffer gets full or your program ends. This makes interactive programs very hard to use: no output or prompt will be printed but your program will still wait for your input. If you type something in, your program continues. If you only ask for one input, this means suddenly all your output will become available after the input.
More information is available here and in the bug report linked from there.
The most convenient fix to add this line as the first line in you main method:
setbuf(stdout, NULL);
(setbuf is a function in stdio.h, so you'll have to include that too if it's not already.) This will turn off buffering for stdout.
Technically, this "hack" should not cause any weird behaviors on other systems. (This is less true of the getchar()
hack used to keep the console window open in Visual Studio!) But you should still probably remove it when you're done developing your code.
You may want to make your compiler more accepting by including the -std=c99
option. To do this:
-std=c99
to the end of the text field.