Basics

Today the goal is to go through the process of creating a simple C program, compiling and running from the terminal, editing in Emacs or Vim, using a Makefile. We'll practice Unix throughout.

We'll start by connecting remotely to Bowdoin server dover and setting up a workspace. Locate the 'Terminal' application and double-click on it to open a terminal. Then type:

bid19628:~ ltoma$ ssh -X dover.bowdoin.edu
Note that we use flag '-X' to enable X11 (windows) forwarding. This will come handy for e.g. if you want to run any application that will open windows remotely on the machine where you sit. Just to test that '-X' worked you could for e.g. run xterm & and expect to see a new terminal window pop up.

[ltoma@dover:~]$ pwd
/home/ltoma
[ltoma@dover:~]$ mkdir cs3225
[ltoma@dover:~]$ cd cs3225/
[ltoma@dover:~/cs3225]$  ls
[ltoma@dover:~/cs3225]$ 
Now we're going to create a link to the data directory. This will appear as if it were your own local directory, but in realiyt it is a symbolic link to a common directory for data. Symbolic links are prefered to copying because they avoid redundancy.
[ltoma@dover:~/cs3225]$ ln -s /mnt/research/gis/DATA .
[ltoma@dover:~/cs3225]$ ls 
DATA
[ltoma@dover:~/cs3225]$ ls -l
total 0
lrwxrwxrwx 1 ltoma cs 22 Sep  8 22:03 DATA -> /mnt/research/gis/DATA
[ltoma@dover:~/cs3225]$ cd DATA/
[ltoma@dover:~/cs3225/DATA]$ ls
DEM      iosortTests  NGDC_CoastalRelief_Images  QuadtreeIntel  SRTM         TIN
grassdb  LIDAR        pics                       QuadtreePPC    TIGER2006SE
[ltoma@dover:~/cs3225/DATA]$ cd ..
[ltoma@dover:~/cs3225]$ 
Now we'll set up a folder for the first C program:
[ltoma@dover:~/cs3225]$ mkdir startup
[ltoma@dover:~/cs3225]$cd startup

Creating a "Hello, world" C program

Now you can start up either Emacs or Vim and create a simple C program to print "hello world". As editors, you can use: xemacs, emacs (the bare bones emacs (no GUI)) or vim. If you have used these before, go ahead and create the program, and name it hello.c. To start xemacs you'll type:
xemacs &
The ampercend at the end of the command means that the command is to be run in the background; the shell prompt does not wait for this command to be finished, instead it starts it and comes right back to you (to really understand the difference you might want to start xemacs without the ampercend at the end).

Once you started xemacs you can start typing the first program:

 #include < stdio.h >

int main() {

   printf("Hello world!\n");
   return 1; 
}
Save this program as "~/cs3225/startup/hello.c". (The symbol "~" expands in Unix to the name of your home directory).

Compiling and running from the terminal

[ltoma@dover:~/cs3225]$ cd startup/
[ltoma@dover:~/cs3225/startup]$ ls
hello.c
[ltoma@dover:~/cs3225/startup]$ ls -l
total 4
-rw-r--r-- 1 ltoma cs 78 Sep  8 22:51 hello.c
[ltoma@dover:~/cs3225/startup]$ 
Note the file is readable and writeable to the owner, and readable only to everybody else. The file is not executable.
  1. run the compiler (gcc) to compile your code into an executable you can run.
    gcc hello.c
      
    I suggest you use the -g flag to include debugging information (stored in a dsym folder, see below):
    gcc -g hello.c
      
  2. If there are errors, it will let you know. Otherwise, it creates an executable called a.out which you can run:
    [ltoma@dover:~/cs3225/startup]$ gcc -g hello.c 
    [ltoma@dover:~/cs3225/startup]$ ls -l
    total 12
    -rwxrwxr-x 1 ltoma cs 7641 Sep  9 12:23 a.out
    -rw-r--r-- 1 ltoma cs   78 Sep  8 22:51 hello.c
    [ltoma@dover:~/cs3225/startup]$ 
    [ltoma@dover:~/cs3225/startup]$ ./a.out 
    hello, world
    [ltoma@dover:~/cs3225/startup]$ 
    
    
    
Note that a.out is executable.

a.out is the default name for the executable. To specify a different name use option "-o":

[ltoma@dover:~/cs3225/startup]$ gcc -g hello.c  -o hello
[ltoma@dover:~/cs3225/startup]$ ./hello 
hello, world
[ltoma@dover:~/cs3225/startup]$ ls 
a.out  hello  hello.c
[ltoma@dover:~/cs3225/startup]$ rm a.out
[ltoma@dover:~/cs3225/startup]$ 
I suggest you also use option "-Wall" to generate all warnings:
[ltoma@dover:~/cs3225/startup]$ gcc -g -Wall hello.c -o hello
[ltoma@dover:~/cs3225/startup]$

Working locally on your laptop/desktop and copying over to dover

If for whatever reason you prefer to work locally on your laptop or desktop: open a new terminal window and keep this window open right next to the window where you are logged in to dover. In this new window type:

bid19628:~ ltoma$ cd Desktop/
bid19628:Desktop ltoma$ mkdir local-cs3225
bid19628:Desktop ltoma$ cd local-cs3225
bid19628:local-cs3225 ltoma$ mkdir startup/
bid19628:startup ltoma$cd startup

Now you should see a new empty folder called local-cs3225 on the Desktop. Start "Aquamacs" or "Xemacs". Start typing your code. Save this program as "~/Desktop/local-cs3225/startup/hello.c". Compile and run as above. At the end, you'll want to copy your files to dover. You'll use 'scp' (stands for secure copy).
bid19628:startup ltoma$ ls
a.out		a.out.dSYM	hello		hello.c		hello.dSYM
bid19628:startup ltoma$ scp hello.c ltoma@dover.bowdoin.edu:~/cs3225/startup/
ltoma@dover.bowdoin.edu's password: 
hello.c                                       100%   78     0.1KB/s   00:00    
bid19628:startup ltoma$