Compiling C programs

The installation in the Linux lab is already set up for compiling C programs either from the command line or from Eclipse (which I recommend). If you're using another type of computer, see the bottom of this document.

Via the command line

  1. Edit and save the file(s) with a text editor such as gedit, jEdit, emacs, or vi.

  2. At the terminal window, navigate into the directory in which the file(s) are saved. You can do this using the cd command, as in “cd directoryName”. If you have saved onto the desktop, you will want “cd Desktop

  3. Compile the file(s) using the gcc command as in “gcc -Wall fileNames”.

  4. If compilation was successful, then you can execute the program by executing the a.out file created by gcc: type “./a.out”.

Via Eclipse

Eclipse is a powerful program written for developing programs of all types. It was originally developed for writing Java programs, but it handles C (and C++) nicely if you include a very nice standard extension called CDT.

  1. First, you need to create your project. Under the File menu's New submenu, select Project. In the first panel, select “C Project” (under C/C++). Enter a name in the next panel, and in the subsequent panels just accept the defaults before finally getting to select Finish. (Important: choose a project name that does not include any space characters — this has been known to cause problems.) Eclipse will prompt you about changing the perspective. You want to do this.

  2. Now create your program by creating a new Source File (using the File menu's New submenu). For the “Source Folder” field, click “Browse…”, select your project name, and then click OK. For the “Source File” field, type the name of a file ending in “.c”, such as count.c. You can now edit the program.

  3. Save your file before attempting to compile or execute it. When compiling the file, Eclipse works with what is saved on disk, not what appears on the screen.

  4. Once you have edited this file to contain the code you want to execute, you will need to build the project. In the Project menu, select Build All (or press control-B). If any errors result, Eclipse will flag them at this point. (In contrast to how it handles Java programs, Eclipse won't notice the errors until you build the project.)

  5. To execute the program, go to the Run menu and select Run (or press control-F11). You only have to use the menubar once. After that, the green Play button in the toolbar will repeat whatever you last ran. For that matter, the hammer button in the toolbar will build the project a second time.

Other computers

on the Linux laboratory computers, but if you want to install it on your own computer, I can suggest the following.

  • For a Linux computer, I first recommend looking through the available packages to see if eclipse-cdt is simply there. If so, simply install that. If not, ensure that gcc and Eclipse are installed by installing the relevant packages. Then in Eclipse, go to the Help menu, and go to the submenu titled Find and Install.... Select “Search for new features,” and enter as the download site. Click Finish. Select CDT when prompted.

  • For Windows, I haven't tried it, but this PDF tutorial from the University of Manitoba seems to describe the process step by step. (I'll be happy to post a different link if somebody finds better instructions for Windows.)

  • For MacOS X, you can obtain gcc through downloading XCode. With XCode, you would go to Preferences, then the Download tab, and then select Install next to Command Line Tools.