Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Practical: Edit-Compile-Run Cycle for C

Programs
When developing programs with command-line tools, you typically iterate the following
steps:

1. Edit: Use an editor to develop the code in your programming language of


choice. Result: a text file with the source code of the program (e.g. hello.c)
2. Compile: Use a compiler to translate the source code into a version understood by the
machine. Result: a binary file with the executable code of the program (e.g. hello)
3. Run: Execute the program on the machine. Result: you see the result of executing
your program (e.g. messages printed by the program)

This section is a practical on how to use common Linux tools to develop simple programs
(we mainly cover C here), to compile a program on the command line and to run the resulting
executable. Most of the information here is typical for this Edit-Compile-Run development
cycle on the command-line. If you use different languages or compilers you naturally need to
adjust the specific commands (and flags), but the overall structure is the same.

This section has evolved out of short summaries about basics of compilation, and more
details on this can be found in the slides here.

Editing
There are a lot of good editors for Linux out there, and some of them are discussed in the
Section called Editors. In this practical we will used emacs as editor, which by default has a
programming language mode for C including highlighting etc.

To get started, download the program called hello.c executing the following command from
the commandline (or using this URL):

$ wget http://www.macs.hw.ac.uk/~hwloidl/Courses/F28HS/srcs/hello.c

You now have the file in your current directory. You can confirm this by typing
$ ls -ltr

and the last line displayed should show the filen hello.c.

Now we want to edit this file using emacs. To do so type

$ emacs hello.c

which will start a new window with the editor and it will display the file hello.c in C mode,
highlighting C keywords. All this program does is to display the string Hello world! in the
terminal; it is intended to be a minimal test program, so that you can focus on the steps
needed to edit, compile and run the program.
Use either mouse or cursor keys to navigate, and the menus for basic operations. Useful
short-cut commands are CTRL-x CTRL-s for saving the current file and CTRL-x CTRL-
c for exiting the editor. A quick-reference card for emacs with the main short-cuts is available
here. Also Chapter 7 in Sobell's Linux Guide has a section on the most important commands.

Compiling
Before we can run the program we need to compile it. This means, we need to run a program
that takes the C program as input, and produces an executable file, i.e. a file in a format that
is understood by the machine. In an IDE the steps of compiling and executing are often
conflated by just clicking at a run button. However, conceptually it is important to separate
the two steps, because they do very different things.

Now, to compile our hello.c program, we execute the following line:

$ gcc -o hello hello.c

This will translate the C program in hello.c into machine code in hello. This machine code
is in binary format, and can be executed directly.

Running
Now that we have generated an executable file, we can run it like this:

$ ./hello
Hello world!

You should see the string Hello world!

You might also like