How to Compile and Run C Programs From the Command Line

Note: These instructions assume you are logged on to Linux on a Computer Science Lab computer.  The same commands will work if you are remotely connected to a Computer Science computer via a terminal window (see Computer Science wiki for how to do so) AND on your own computer if have correctly installed gcc (the compiler).

A little background...

The compiler we are using this quarter is called gcc.  We will be using specific compiler options (or flags) to control the behavior of the compiler.  Note I will be using these flags when grading all of your programs.  Therefore, you must use these same flags to be sure your code will compile for me when I grade it.  If you don't, your code may compile and work for you but not for me.  If your code does not compile for me you will recieve a grade of zero for the assignment.

The flags may be entered in any order after the compiler (gcc) and before your C-source file(s).  Here are some of the flags you may be using along with a brief description of the affect they have on the compiler's behavior:

        -ansi: Affects the particular version of the the C-language to use (see ANSI C, a.k.a. C89).

        -pedantic: Tells the compiler to give its most clear and explicit error and warning messages.

        -Wall: Tells the compiler to display all warnings, even those considered relatively minor and unimportant.

        -Werror: Tells the compiler to treat warnings as errors and NOT generate and executable when any warnings occur.

        -lm: Causes the compiler to link a library that provides certain Standard Library functions.

To compile:

  1. Open a Terminal window (Select Applications->Accessories->Terminal on the menu bar)
  2. Change directory (cd-command) to the directory containing the C-source file(s) you wish to compile.
  3. Verify that the C-source file(s) are present using the ls-command.
  4. Assumming you have a single C-source file called mySource.c, you would use the following command go compile it:
            gcc -ansi -pedantic -Wall -Werror -lm mySource.c

If you have more that one source file that makes up your single program you would simply list them all, separated with spaces, in the command above.  For example, assumming you have mySource1.c, mySource2.c, and mySource3.c, the command to compile them into a single program would be:

            gcc -ansi pedantic -Wall -Werror -lm mySource1.c mySource2.c mySource3.c
  1. If you have compiler errors you must fix them now.  See How To Fix Compiler Errors to do so and then go back to Step 4,  otherwise continue to the next section on how to run your program.
To run:
  1. First of all, don't make this harder than it already is!  Be sure you compiled without error.  If you did not, and have had a previously successful compile of an earlier verions of the same souce or even different source in the same directory, you will be running the wrong program. This usually lead to much confusion, frustration, and lost minutes or hours of your life and lower grades.
  2. Once you are sure you had a successful compile you can run the following command to run your program:
            a.out

This may look odd to you so a little explaination may help. First of all, by default the compiler names the executable a.out, and secondly, your Linux accounts, by default, do not look in the current directory for commands.  The './' prepended to the 'a.out' tells the command shell your running in to look in the current directory. 

Two ways to name your executable something other than a.out:
  1. You can use mv-command to rename a.out to what ever you wish.
  2. You can use another compiler flag, '-o',  to tell gcc to name it what ever you wish as follows:
            gcc -ansi pedantic -Wall -Werror -lm -o myBaby mySource1.c mySource2.c mySource3.c

This will result in the executable produced by the compiler being named myBaby instead of a.out.  To run it you would use the following command:

            myBaby