Compiling and running a C/C++ program from the terminal

We'll go through the process of creating a simple C program using an editor, compiling and running from the terminal using a Makefile. We'll practice Unix throughout.

Setting up a folder

Locate the 'Terminal' application and double-click on it to open a terminal. I'll create a folder called "compsci":

[ltoma@lobster:~]$ pwd
/home/ltoma
[ltoma@lobster:~]$ mkdir compsci
[ltoma@lobster:~]$ cd compsci/
[ltoma@lobster:~/compsci]$  ls
[ltoma@lobster:~/compsci]$ 
Inside it, I'll set up a folder for the first C program:
[ltoma@lobster:~/compsci]$ mkdir hello
[ltoma@lobster:~/compsci]$cd hello

Creating a "Hello, world" program

Now we'll use an editor to create a simple C program to print "hello world". You can use emacs (e.g. xemacs, Aquamacs for Apple), vim, Sublime, nano, [..]. Open the editor, create a new file, and name it hello.c. For example, to start xemacs from the terminal you'll type xemacs &; on a Mac, clicking on the editor iconwill also work. (Note: 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 understand the difference try starting xemacs without the ampercend at the end)).

Once you open the editor you can start typing the first program:

 #include < stdio.h >

int main() {

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

Compiling and running from the terminal

[ltoma@lobster:~/compsci]$ cd hello/
[ltoma@lobster:~/compsci/hello]$ ls
hello.c
[ltoma@lobster:~/compsci/hello]$ ls -l
total 4
-rw-r--r-- 1 ltoma cs 78 Sep  8 22:51 hello.c
[ltoma@lobster:~/compsci/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:
    [ltoma@lobster:~/compsci/hello]$ gcc -g hello.c 
    [ltoma@lobster:~/compsci/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@lobster:~/compsci/hello]$ 
    
    Note that a.out is executable. You can run it:
    [ltoma@lobster:~/compsci/hello]$ ./a.out 
    hello, world
    [ltoma@lobster:~/compsci/hello]$ 
    

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

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