Friday, October 17, 2008

Dividing C programs into files and run them with makefile

well¸ with my experience of large scale C programs(a Mobile Operative System that can send SMS), I would really recommend you to divide your program into files because without it you will just go officially nuts ...

How to Divide a Program between Several Files

Where a function is spread over several files, each file will contain one or more functions. One file will include main while the others will contain functions which are called by others. These other files can be treated as a library of functions.

Programmers usually start designing a program by dividing the problem into easily managed sections. Each of these sections might be implemented as one or more functions. All functions from each section will usually live in a single file.

Where objects are implemented as data structures, it is usual to to keep all functions which access that object in the same file. The advantages of this are;

*The object can easily be re-used in other programs.
*All related functions are stored together.
*Later changes to the object require only one file to be modified.

Where the file contains the definition of an object, or functions which return values, there is a further restriction on calling these functions from another file. Unless functions in another file are told about the object or function definitions, they will be unable to compile them correctly.

The best solution to this problem is to write a header file for each of the C files. This will have the same name as the C file, but ending in .h. The header file contains definitions of all the functions used in the C file.

Whenever a function in another file calls a function from our C file, it can define the function by making a #include of the appropriate .h file.
Organisation of Data in each File

Any file must have its data organised in a certain order. This will typically be:

1.A preamble consisting of #defined constants, #included header files and typedefs of important datatypes.
2.Declaration of global and external variables. Global variables may also be initialised here.
3.One or more functions.

The order of items is important, since every object must be defined before it can be used. Functions which return values must be defined before they are called. This definition might be one of the following:

*Where the function is defined and called in the same file, a full declaration of the function can be placed ahead of any call to the function.
*If the function is called from a file where it is not defined, a prototype should appear before the call to the function.

A function defined as

float find_max(float a, float b, float c) { /* etc ... ... */

would have a prototype of

float find_max(float a, float b, float c);

The prototype may occur among the global variables at the start of the source file. Alternatively it may be declared in a header file which is read in using a #include.

It is important to remember that all C objects should be declared before use.
Compiling Multi-File Programs

This process is rather more involved than compiling a single file program. Imagine a program in three files prog.c, containing main(), func1.c and func2.c. The simplest method of compilation (to produce a runnable file called a.out) would be

cc prog.c func1.c func2.c

If we wanted to call the runnable file prog we would have to type

cc prog.c func1.c func2.c -o prog

In these examples, each of the .c files is compiled, and then they are automatically linked together using a program called the loader ld.
Separate Compilation

We can also compile each C file separately using the cc -c option. This produces an object file with the same name, but ending in .o. After this, the object files are linked using the linker. This would require the four following commands for our current example.

cc -c prog.c cc -c func1.c cc -c func2.c ld prog.o func1.o func2.o -o prog

Each file is compiled before the object files are linked to give a runnable file.

The advantage of separate compilation is that only files which have been edited since the last compilation need to be re-compiled when re-building the program. For very large programs this can save a lot of time.

The make utility is designed to automate this re-building process. It checks the times of modification of files, and selectively re-compiles as required. It is an excellent tool for maintaining multi-file programs. Its use is recommended when building multi-file programs.
Using make with Multi-File Programs

We have already used make to build single file programs. It was really designed to help build large multi-file programs. Its use will be described here.

Make knows about `dependencies' in program building. For example;

*

We can get prog.o by running cc -c prog.c.
*

This need only be done if prog.c changed more recently than prog.o.

make is usually used with a configuration file called Makefile which describes the structure of the program. This includes the name of the runnable file, and the object files to be linked to create it. Here is a sample Makefile for our current example

# Sample Makefile for prog
#
# prog is built from prog.c func1.c func2.c
#
# Object files (Ending in .o,
# these are compiled from .c files by make)
OBJS = prog.o func1.o func2.o
# Prog is generated from the object files
prog: $(OBJS)
$(CC) $(CFLAGS) -o prog $(OBJS)
# ^^^ This space must be a TAB.
# Above line is an instruction to link object files

This looks cluttered, but ignore the comments (lines starting with #) andthere are just 3 lines.

When make is run, Makefile is searched for a list of dependencies. The compiler is involved to create .o files where needed. The link statement is then used to create the runnable file.

make re-builds the whole program with a minimum of re-compilation, and ensures that all parts of the program are up to date. It has many other features, some of which are very complicated.

For a full description of all of these features, look at the manual page for make by typing

man make

source

No comments: