Multi File Programming
Multi File Programming
Generally, small programs (150 lines of code or less) are done using one single file All code of the program we have
written resides in a single program file. We written programs using various modules /functions but all those
module/functions were in a single file. Variable, functions prototype even declared outside the main function but they
are written in the same file. This single file approach to implement a program is good for understanding the concepts
of basic programming language at beginner level in graduation.
But software industries didn’t implement software codes of the problem in single file program way. As large
programs (specially, 1000 lines of code or larger) become quite unmanageable. This is due to periodic maintenance
(adding, deleting and modifying features) and also for understanding what the program does. It is for these reasons
that large programs need to break down into multiple files. As a software developer we may come across situations
where we need to organize our code into well-structured form that help to manage and maintain programs in easier
way. Thus, this can be achieved by dividing the code of a program further into into multiple files. Such way of
writing the programs are called, multi-file programming. Multi-file programs are such programs in which program
codes are distributed into multiple files communicating with each other.
It is better realistic and practical approach of advanced programming where we want loosely coupled code
functions/modules to communicate with each other. This approach of coding increases reusability and when you
want to change/add anything in a module, you will only have to change/add it in that particular program file. Several
peoples can work on same lager complex project. Thus in such case , each coder is responsible for designing
designated modules; therefore separate file for each programmer, are convenient to code and then subsequently
integrate.
File categories
In “C”, most large programs are written using functions. Functions have 1)
prototypes, 2) functions’ definition, and 3) call to functions.
1) Functions’ definitions are stored in function implementation file (or simply “implementation” file).
2) Functions prototypes are stored in a header (or simply “include” file).
3) The call to functions are stored in the file containing the main function call (main() reserved word). This
file is generically referred to as “driver” file – since it “drives” all the functionalities.
A large program may have one or more header file(s); one or more implementation file(s). However, it would
ALWAYS have one and ONLY one “driver” file.
An example: Assume you like to write a multi-file program with 2 functions “add” and “subtract”. Here is
how it would look like:
addsub_header.h addsub_impl.c
#include <stdio.h> #include “addsub_header.h”
int add(int,int); int add(int a, int b)
int sub(int,int); {
return a+b;
}
NOTE: compiling using “-c” option creates an object file with the same name but with “.o” file
extension
Compiling easier (using a shell program) During development of large programs, most often programs get
compiled numerous times before they become stable. Issuing the compile commands individually could become a
laborious task.
For this reason, the compile commands are “bundled” in an executable file (called shell script) and run in a
collective (batch) mode. Normally, this file for compiling purposes is called a “build” file.
In a windows environment, the shell scripts files MUST have a “.bat” file extension e.g. build.bat (“.bat” is
short for “batch” files – meaning the commands will run as a collection or in a batch mode).
build.bat
gcc -c addsub_impl.c –o my_own_impl
gcc -c addsub_driver.c my_driver
gcc my_own_impl my_driver –o my_executable_file