Professional Documents
Culture Documents
Practical Cycle of C Program
Practical Cycle of C Program
Programs
When developing programs with command-line tools, you typically iterate the following
steps:
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
$ 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.
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!