Last Updated: 2015-06-09 Tue 11:53

Setting up your Environment to Compile C Programs

Table of Contents

1 Unixy Systems and GCC

You will need to get gcc, the GNU compiler collection. This is the suite of programs that we will use to compile C programs. You will also need to have access to the bash command shell. Don't try to download these from the gcc homepage. The following sections are devoted to setting up your environment to have a unixy feel: a command line interpreter and compiler.

2 Setting up Windows/Cygwin

Windows isn't based on unix, but Cygwin provides a fairly complete Unix interface to Windows. By following the below steps, you will set up a shell, compiler, and tools required to work with C programs in Windows.

Tell the System where HOME is located

Unix systems almost always have a notion of a home directory. Windows does as well but the following steps will ensure that Cygwin has the same notion of home as Windows.

Set the HOME Environment Variable to be your standard windows home directory. My windows home directory is C:\Users\kauffman. Use the following pictures to set your HOME variable so that the Cygwin starts in the right place (tested on Windows 8).

e1.png e2.png

e3.png e4.png

Once you hit OK to set the new environment variable and OK again to accept all the environment variables.

Download Cygwin Setup

Go to the Cygwin install page and look for one of two files

  • For newer 64-bit systems, download setup-x86_64.exe
  • For older 32-bit systems, download setup-x86.exe

If you are not sure whether your system is 32-bit or 64-bit, have a look at your system properties.

system-properties.png

Run the Setup Program

Run the setup-x86_64.exe or setup-x86.exe program. The default options for most choices should work fine. The only choices you should need to make are to pick a site to download from. Georgia Tech's site is fast and works well.

install01.png install02.png

install03.png install04.png

install05.png install06.png

install07.png install08.png

install09.png install10.png

Start A Cygwin shell

Start a Cygwin shell by either clicking on a shortcut icon or pressing the windows key and typing "Cygwin'

shell1-1.png shell1-2.png

shell2.png

The unix shell is a beautiful, simple place to live and get work done. One of the goals of our class is to gain basic familiarity with the shell and that starts here.

Check all has gone well by printing the working directory which is the shell command pwd.

pwd.png

If you set your home directory correctly (Tell the System where HOME is located), your shell should start in your home directory which is listed with a tilde symbol (~) as part of the prompt. The pwd command reported I am in the directory /cygdrive/c/Users/kauffman which looks a little funny but is correct.

  • All windows directories are mounted starting at /cygdrive in cygwin
  • My home directory on Windows is C:\Users\kauffman which translates to c/Users/kauffman in Cygwin.

If your shell did not start in you windows home directory you probably need to set your HOME environment variable and then start another shell.

Install Additional Required Packages

Next change directory (cd command) to the folder in which you downloaded setup.exe. I downloaded it to Users/kauffman/Downloads and my shell started in Users/kauffman so I would run the command cd Downloads

cd.png

You can check that you arrived in the right spot by doing pwd and checking that the setup.exe program is present by listing the contents of a directory (ls command).

ls.png

The cygwin system is installed but does not come be default with a compiler or other necessary tools for C development. setup.exe can be used to install these additional programs. Copy the commands below into your shell: highlight, them, copy, then right-click in the shell and select paste (standard keyboard shortcuts like Ctrl-C and Ctrl-V have different meanings in the Cygwin shell so when in doubt, right click).

For 64-bit machines with the setup-x86_64.exe installer, paste the following command into the shell.

./setup-x86_64.exe -q \
-P gcc-core    \
-P ggc-g++     \
-P colorgcc    \
-P diffutils   \
-P CUnit       \
-P xxd         \
-P joe         \
-P emacs       \
-P vim         \
-P nano        \
-P cygwin-doc  \
-P make        \
-P gdb         \
-P cgdb        \
-P zip         \
-P unzip       \
-P openssh     \
-P bc

For 32-bit machines with the setup-x86.exe installer, paste the following command into the shell.

./setup-x86.exe -q \
-P gcc-core    \
-P ggc-g++     \
-P colorgcc    \
-P CUnit       \
-P diffutils   \
-P xxd         \
-P joe         \
-P emacs       \
-P vim         \
-P nano        \
-P cygwin-doc  \
-P make        \
-P gdb         \
-P cgdb        \
-P zip         \
-P unzip       \
-P openssh     \
-P bc

After pasting, you may need to press enter. The installer will start running and install the new packages listed above. This will make a C compiler and other utilities available.

install-packages.png

At this point, if all has gone according to plan, the compiler should be installed. Type gcc to make sure it's there. Below is proof that gcc is present (though it's not happy as we haven't given it a program to compile).

gcc-is-there.png

If you get something like Command not found or Program not installed then something is amiss and you should try installing the programs again following the steps above.

3 Setting Up Mac OS X

To get gcc, use one of following routes.

For a terminal, use Terminal.app which is on all OS X's. It is a unix command shell. Ensure that you have gcc around by opening a terminal and typing

gcc --version

The terminal should indicate a version such as the below picture

Mountain-Lion-gcc-llvm-verification.png

If a message like "not found" appears, something has gone haywire and you'll want to get some help.

4 Setting up Linux

  • I'll assume you know what you are doing if you're running your own Linux system.
  • Use favorite terminal emulator (xterm, gnome-terminal, rxvt, etc.)
  • Use a package manager to install gcc, probably something like
    sudo apt-get install gcc
    

As an alternative to the Windows or Mac Methods, you can use VirtualBox to set Linux up on those platforms.

5 Getting a Suitable editor

Once you have the compiler and shell set up on your machine, you will need a good text editor. This is a tool to write (surprise) text files which will be the source code for our programs. The built-in Notepad on Windows and TextEdit on Mac will not make your life easy for programming. However, there are tons of text editors with features that will make programming easier. Here are a few suggestions.

GUI-Based

jEdit
Available on all platforms, built by programmers for programmers, intuitive, colorful, reasonable power, and free.
NotePad++
Windows only, free software, many students swear by it.
TextPad
Windows only, free for eval, pay to keep, solid editor for programming and what I used when forced to use only Windows tools
TextMate
Mac only, nonfree, award-winning.

Traditional

nano
Extremely bare-bones editor but it runs in the terminal on most unix machines (like zeus and mason). Avoid this if you can, use it in a pinch.
vim
The classic editor for unix, probably also available on Mac by default. Terminal only editing, highly unintuitive but extremely powerful. Free.
emacs
The editor I use, though it's more like a way of life than an editor. Somewhat unintuitive, but once you get used to it, you'll never be the same. Comes in Windows and Mac flavors. Free and unrivaled in power. 1

The last three editors, nano, vim, and emacs, are installed with cygwin and are available by typing the editor name on the command prompt. None of them are graphical by default so there will be a learning curve.

A note on IDEs: We'll be focusing on learning the steps to produce programs: editing, compiling, debugging, running on the terminal. Integrated Development Environments (IDEs) like Microsoft Visual Studio, Apple's XCode, and Eclipse will be a major hindrance to this effort as they hide many of those details. Those tools have their place for professional software engineers who can dive into the details if the need arises, but since we're learning this stuff the first time, we need to be concerned with such details.

To that end, no help will be given diagnosing problems in an IDE. Stick to a text editor and the gcc command line interface.

6 Getting around in Unix

In my demos, I'll interact with the computer on the command line in the bash shell. I suggest you try to do the same.

When you start your shell, you'll get a prompt of some sort . For instance, when I log into my linux desktop, I see

kauffman@lila:~$

which gives my user name kauffman, the machine I'm logged into lila, and the current directory ~ which means "home." The dollar sign $ is where I can start typing. You can set the prompt to other things which are more to your taste such as

lila [~]%

which is what I use (just machine and current directory). Peruse the internet for tricks on how to change your prompt.

In the world of the command line, you type (surprise) commands followed by options to modify their behavior and operands on which they will execute. The canonical example is the ls command for listing the contents of a directory.

  lila [~]% ls
  bin        examples.desktop  hw2.html~  Pictures   test.c      web
  Desktop    feat-select       hw.html    Public     test.c~     x.org
  Documents  glu21             Music      R          texmf
  Downloads  hello             newdir     teaching   Ubuntu One
  Dropbox    hw2.html          packages   Templates  Videos

You can change the behavior of ls to list additional information about files by giving the long option: ls -l.

  lila [~]% ls -l
  total 140K
  drwxr-xr-x  2 kauffman kauffman 4.0K Aug 13 10:33 bin
  drwxr-xr-x  2 kauffman kauffman 4.0K Jul 23 18:04 Desktop
  drwxr-xr-x  3 kauffman kauffman 4.0K Aug  8 10:34 Documents
  drwxr-xr-x  2 kauffman kauffman 4.0K Aug 10 11:57 Downloads
  drwx------ 11 kauffman kauffman 4.0K Jul 27 17:28 Dropbox
  -rw-r--r--  1 kauffman kauffman 8.3K Jul 23 18:02 examples.desktop
  drwxr-xr-x  5 kauffman kauffman 4.0K Aug 13 13:28 feat-select
  drwxr-xr-x  2 kauffman kauffman 4.0K Aug  6 18:22 glu21
  -rwxr-xr-x  1 kauffman kauffman 8.3K Aug 13 16:34 hello
  -rw-r-----  1 kauffman kauffman 4.4K Aug  8 15:04 hw2.html
  -rw-r-----  1 kauffman kauffman 6.1K Aug  8 15:01 hw2.html~
  -rw-r-----  1 kauffman kauffman 4.4K Aug  8 15:04 hw.html
  drwxr-xr-x  2 kauffman kauffman 4.0K Jul 23 18:04 Music
  drwxr-xr-x 11 kauffman kauffman 4.0K Aug  2 14:35 newdir
  drwxr-xr-x  3 kauffman kauffman 4.0K Aug 13 11:16 packages
  drwxr-xr-x  2 kauffman kauffman 4.0K Jul 24 17:56 Pictures
  drwxr-xr-x  2 kauffman kauffman 4.0K Jul 23 18:04 Public
  drwxr-xr-x  3 kauffman kauffman 4.0K Aug  9 16:21 R
  drwxr-x--x  4 kauffman kauffman 4.0K Aug 13 13:56 teaching
  drwxr-xr-x  2 kauffman kauffman 4.0K Jul 23 18:04 Templates
  -rw-r--r--  1 kauffman kauffman  149 Aug 14 11:56 test.c
  -rw-r--r--  1 kauffman kauffman  154 Aug 13 16:34 test.c~
  drwxr-x--x  5 kauffman kauffman 4.0K Aug 13 10:58 texmf
  drwxrwxr-x  2 kauffman kauffman 4.0K Aug  3 11:28 Ubuntu One
  drwxr-xr-x  2 kauffman kauffman 4.0K Jul 23 18:04 Videos
  drwxr-sr-x 11 kauffman kauffman 4.0K Aug  2 18:23 web
  -rw-r--r--  1 kauffman kauffman 7.8K Aug  9 20:56 x.org

This gives file permissions, owner, size, date modified, and other information on the files.

The shell always has a working directory in which it exists at any given time. To find out where you are, use pwd. To change where you are, use cd.

lila [~]% pwd
/home2/kauffman
lila [~]% cd bin
lila [bin]% pwd
/home2/kauffman/bin
lila [bin]% cd /home2/
lila [home2]% pwd
/home2
lila [home2]% cd /home2/kauffman/bin
lila [bin]% pwd
/home2/kauffman/bin

A couple shortcuts are useful: two periods, .. refer to the directory immediately above and tilde, ~ refers to your home directory.

lila [bin]% cd ..
lila [~]% cd ../..
lila [/]% pwd
/
lila [/]% cd ~
lila [~]% pwd
/home2/kauffman

In graphical user interfaces (GUI, mouse/folders/icons/etc.), we usually move files around in folders. Folders are directories. You can make new directories on the command line with mkdir. The following creates a new directory named my-new-folder.

lila [~]% mkdir my-new-folder
lila [~]% ls
first-program:

my-new-folder:

The new directory will also show up in any GUI you run on your system.

new-folder.png

There are a wealth of other commands with which you'll need to familiarize yourself. I suggest running through a basic online unix tutorial such as this one: http://www.ee.surrey.ac.uk/Teaching/Unix/.

One thing that you may find very useful are the documentation commands: man for manual pages and apropos to search manual pages. man will present information on a command on the system. For example

lila [~]% man ls
LS(1)                                          User Commands                                         LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List  information  about the FILEs (the current directory by default).  Sort entries alphabetically
       if none of -cftuvSUX nor --sort is specified.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

...

gives displays all the options and usage of the ls command. apropos allows you to search for commands by keyword. Since we'll be compiling, you may try the following

lila [~]% apropos compile
c++ (1)              - GNU project C and C++ compiler
c89 (1)              - ANSI (1989) C compiler
c89-gcc (1)          - ANSI (1989) C compiler
c99 (1)              - ANSI (1999) C compiler
c99-gcc (1)          - ANSI (1999) C compiler
cc (1)               - GNU project C and C++ compiler
ckbcomp (1)          - compile a XKB keyboard description to a keymap su...
dh_python2 (1)       - calculates Python dependencies, adds maintainer s...
f95 (1)              - GNU Fortran compiler
foomatic-compiledb (1) - Compile the Foomatic printer/driver database
g++ (1)              - GNU project C and C++ compiler
g++-4.6 (1)          - GNU project C and C++ compiler
gcc (1)              - GNU project C and C++ compiler
gcc-4.6 (1)          - GNU project C and C++ compiler
gfortran (1)         - GNU Fortran compiler
gfortran-4.6 (1)     - GNU Fortran compiler
...

Notice the gcc entry above: this is a command that we will be using to compile C programs.

Note: Cygwin may not have indexed all the manual pages yet so apropos might not work. If you get the message Nothing appropriate, run the command

/usr/sbin/makewhatis

7 Commands are programs (mostly)

"Commands" like ls are just programs that already exist on the system. To see what directory the program lives you can do

lila [~]% which ls
/bin/ls
lila [~]% file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), 
for GNU/Linux 2.6.24, BuildID[sha1]=0x214a38d0db472db559f0dabf0ae97f82fea83e03, stripped

Some commands are not programs but are built directly into the shell, like cd.

lila [~]% which cd
lila [~]% type cd
cd is a shell builtin
lila [~]% type ls
ls is hashed (/bin/ls)

The shell allows you to alias commands for convenience. For example, we may always want to run ls as ls -F which shows directories with a / and executable programs with a *.

lila [bin]% ls
avgstd  ckshutdown  gcal    gmail   myenv   ncol     wgetpdb
ckjoin  confusion   getpdb  mntglu  myenv~  umntglu  wmname
lila [bin]% ls -F
avgstd*  ckshutdown*  gcal*    gmail*   myenv*  ncol*     wgetpdb*
ckjoin*  confusion*   getpdb*  mntglu*  myenv~  umntglu*  wmname*
lila [bin]% alias ls='ls -F'
lila [bin]% type ls
ls is aliased to `ls -F'
lila [bin]% ls
avgstd*  ckshutdown*  gcal*    gmail*   myenv*  ncol*     wgetpdb*
ckjoin*  confusion*   getpdb*  mntglu*  myenv~  umntglu*  wmname*
lila [bin]%

8 Compiling C Code

We will be creating new commands by compiling C code to create programs. This will primarily be done using gcc which is the GNU compiler collection. It is actually a series of programs with tons of options but for most of the course, we'll only need its most basic features.

You can get a program to compile in first-program.zip.

Warning: you'll need to extract the archive in order to access it's contents. This can be done on the command line using the command unzip as in

lila [~]% unzip first-program.zip

or using a GUI. Below is a picture of how to extract archives on Windows 8.

extract1.png extract2.png

extracted.png

If we have a program in ~/first-program/hello.c, I can look at it, compile it, and run it using the following.

lila [~]% cd first-program/
lila [first-program]% ls
hello.c
lila [first-program]% cat hello.c
#include <stdio.h>

int main() {
  printf("Hello World\n");
}
lila [first-program]% gcc -o hello-program hello.c
lila [first-program]% ls
hello.c hello-program
lila [first-program]% ./hello-program
Hello World

Breaking this down, each line means

lila [~]% cd first-program/ Change to a directory
lila [first-program]% ls List what's in that directory (C code for the program)
lila [first-program]% cat hello.c Print (cat) the contents of the hello.c file to the screen
lila [first-program]% gcc -o hello-program hello.c Compile the C code with gcc, name the output program hello-program
lila [first-program]% ls List what's in the directory (C code and compiled program)
lila [first-program]% ./hello-program Run the program, ./hello-program means "the program in the current directory"

If you can successfully compile a test program in this way, you're well on your way for the course.

9 Creating Zip Archives

Compressed archives are files that accomplish two things

  • They agglomerate several files into a single file for ease of transfer
  • They compress the contents so that the archive takes less space than the original version

All programming assignments will be submitted by creating a compressed archive and uploading the archive to Blackboard.

There are a variety of formats of compressed archive but we will use the ZIP format as utilities for dealing with the ZIP format are almost universally available without additional work. *Failure to submit in the ZIP format may result in a loss of credit on programming assignments.*

Creating a Zip in Unix (Windows/Cygwin, Mac OS X, Linux)

If you installed Cygwin properly or have another sensible Unix environment, you will have access to the zip and unzip programs which are useful for creating archives.

To zip a whole folder, navigate to the directory where the folder exists and use the command

zip zip-file-name -r directory-name

Here is a picture of zipping up the first-program directory which is done using the command

zip first-program.zip -r first-program

The subsequent commands verify that the zip was created and show its contents (with the unzip -l first-program.zip command). To the right is a picture of the visual view of the same directory which shows the folder and highlights the zip file that was created from it.

make-zip.png has-zip.png

A typical invocation is

zip ckauffm2-hw1.zip -r ckauffm2-hw1

which will make a compressed archive of all the files (programs/data) in a homework directory.

Creating Archives using Windows GUI

Windows 7 and 8 come with built-in support for creating and dealing with ZIP files. You can use the Windows GUI with to create an archive using the following steps.

  • Find the folder containing your programming assignment
  • Right click on the directory
  • Pick Send To -> Compressed Folder (.zip)

You should now have a compressed version of your folder.

compress1.png

compress2.png

Creating a Zip using Mac OS X GUI

Mac OS X users should also be able to create a zip using the their GUI.

  • Right click (or hold the control key and click) on a folder in Finder.
  • From the context menu, select Compress "Folder name" (may be called "Create Archive of …" instead)
  • A zip archive of your folder should appear alongside the original folder

zip-file-folder-on-Mac.png

The following link contains additional instructions and a video of this process: http://www.hacktrix.com/zip-file-folder-on-mac-os-x

Footnotes:

1

Steve Yegge's Essay "Tour de Babel" gives a crass but entertaining account of why learning emacs may be worth your while and also why we're learning C as a language.


Author: Chris Kauffman (kauffman@cs.gmu.edu)
Date: 2015-06-09 Tue 11:53