Compiling and running a C program from the terminal

Let's go through the process of creating a simple C program using Emacs or Vim, compiling and running from the terminal.

Open the Terminal app and create a directory for this class:

[ltoma@dover:~]$ pwd
/home/ltoma
[ltoma@dover:~]$ mkdir CompGeom
[ltoma@dover:~]$ cd CompGeom
[ltoma@dover:~/CompGeom]$  ls
[ltoma@dover:~/CompGeom]$ 
We'll set up a folder for the first C program:
[ltoma@dover:~/CompGeom]$ mkdir hello
[ltoma@dover:~/CompGeom]$cd hello

Creating a "Hello, world" C program

You can start up an editor to create a simple C program to print "hello world". As editors, you can use: aquamacs (apple version of emacs), xemacs (standard x-emacs), emacs (the bare bones emacs with no GUI) or vim, or (apple specific) Sublime. 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 "~/CompGeom/hello/hello.c". (The symbol "~" expands in Unix to the name of your home directory).

Compiling and running from the terminal

[ltoma@dover:~/CompGeom]$ cd hello/
[ltoma@dover:~/CompGeom/hello]$ ls
hello.c
[ltoma@dover:~/CompGeom/hello]$ ls -l
total 4
-rw-r--r-- 1 ltoma cs 78 Sep  8 22:51 hello.c
[ltoma@dover:~/CompGeom/hello]$ 
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:~/CompGeom/hello]$ gcc -g hello.c 
    [ltoma@dover:~/CompGeom/hello]$ 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:~/CompGeom/hello]$ 
    [ltoma@dover:~/CompGeom/hello]$ ./a.out 
    hello, world
    [ltoma@dover:~/CompGeom/hello]$ 
    
    
    
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:~/CompGeom/hello]$ gcc -g hello.c  -o hello
[ltoma@dover:~/CompGeom/hello]$ ./hello 
hello, world
[ltoma@dover:~/CompGeom/hello]$ ls 
a.out  hello  hello.c
[ltoma@dover:~/CompGeom/hello]$ rm a.out
[ltoma@dover:~/CompGeom/hello]$ 
I suggest you also use option "-Wall" to generate all warnings:
[ltoma@dover:~/CompGeom/hello]$ gcc -g -Wall hello.c -o hello
[ltoma@dover:~/CompGeom/hello]$

Connecting remotely on one of the Linux servers on campus (dover and foxcroft)

To connect to Bowdoin server dover, find 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.

Now you are on dover. You can do anything you would on your laptop. You can access dover from anywhere on campus, and your home directory is automatically backed up.

Working locally on your laptop and copying over to dover

If you prefer to work locally on your laptop: open a terminal and cd to the place where you have your folder that you want to copy over.

To copy to dover you'll use use 'scp' (stands for secure copy).

bid19628:hello ltoma$ scp hello.c ltoma@dover.bowdoin.edu:~/CompGeom/hello/
ltoma@dover.bowdoin.edu's password: 
hello.c                                       100%   78     0.1KB/s   00:00    
bid19628:hello ltoma$