0% found this document useful (0 votes)
3 views

Multi File Programming

The document discusses the importance of multi-file programming for managing large software projects, emphasizing that single-file programs are suitable for small, beginner-level projects. It outlines the structure of multi-file programs in C, including the roles of header files, implementation files, and driver files, as well as methods for compiling these programs. Additionally, it introduces the concept of using shell scripts to streamline the compilation process in a batch mode for easier development of large programs.

Uploaded by

2023kucp1012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Multi File Programming

The document discusses the importance of multi-file programming for managing large software projects, emphasizing that single-file programs are suitable for small, beginner-level projects. It outlines the structure of multi-file programs in C, including the roles of header files, implementation files, and driver files, as well as methods for compiling these programs. Additionally, it introduces the concept of using shell scripts to streamline the compilation process in a batch mode for easier development of large programs.

Uploaded by

2023kucp1012
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Developing code as multi-file program

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;
}

addsub_driver.c int sub(int a, int b)


#include “addsub_header.h” {
int main() return a-b;
{ printf(“%i”,add(2,3)); }
printf(“%i”,sub(9,4));
return 0;
}

Compiling the multi-file program


Only the programs with the “.c” extensions need to be compiled. There are 2 ways of compiling the programs:

Method 1: gcc -c implementation_file_name.c <ENTER>


gcc -c driver_file_name.c <ENTER>
gcc implementation_file_name.o driver_file_name.o <ENTER>

For example: gcc -c addsub_impl.c


gcc -c addsub_driver.c
gcc addsub_impl.o addsub_driver.o The result is “a.exe” file.

NOTE: compiling using “-c” option creates an object file with the same name but with “.o” file
extension

Method 2: gcc -c implementation_file_name.c –o an_object_file_name <ENTER>


gcc -c driver_file_name.c –o a_driver_file_name <ENTER>
gcc my_own_impl my_driver –o an_execuatble_file_name <ENTER>

For example: 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 // create “my_executable_file.exe” file.
NOTE: Although, in this method 2 the “.o” file extension is not necessary, nonetheless it is a practice
to name the object files with “.o” extensions for easier account keeping.

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

You might also like