2. Developing a Large Program
Developing a large Program
– We can split source codes into several ~. h and ~.c files
– There are many merits of several small files instead of one big file
• Easy to maintain source codes
• Easy to co-work with other programmers
• Save time when compiling: You can just compile ~.c files which are
modified not all of source codes.
2
3. 3
Modulization
Split source code into separate files
#define TWO 2
typedef void VOID ;
int g;
VOID func1(VOID) ;
VOID func2(VOID ) ;
VOID main(VOID )
{
func1() ;
func2() ;
g *= TWO ;
}
file.c
VOID func1(VOID )
{
func2() ;
g += TWO ;
}
VOID func2(VOID )
{
func1() ;
g -= TWO ;
}
To be written
by friend 1
To be written
by friend 2
To be written
by me
4. 4
Modulization
Split source code into separate files
– Make ~.c files for each function
– Add #define, declaration, function prototypes, extern objects
Prototype of Functions
#define TWO 2
typedef void VOID ;
int g;
VOID func1(VOID) ;
VOID func2(VOID) ;
VOID main(VOID)
{
func1() ;
func2() ;
g *= TWO ;
}
#define TWO 2
typedef void VOID ;
extern int g;
VOID func2(VOID) ;
VOID func1(VOID)
{
func2() ;
g += TWO ;
}
#define TWO 2
typedef void VOID ;
extern int g;
VOID func1(VOID) ;
VOID func2(VOID)
{
func1() ;
g -= TWO ;
}
file1.c file2.c file3.c
5. 5
Modulization
Split source code into separate files – Problem
– When you want to modify #define or typedef definitions
• You should modify all of source files
=> Solution : Use Header file
#define TWO 2
typedef void VOID ;
int g;
VOID func1(VOID) ;
VOID func2(VOID) ;
VOID main(VOID)
{
func1() ;
func2() ;
g *= TWO ;
}
#define TWO 2
typedef void VOID ;
extern int g;
VOID func2(VOID) ;
VOID func1(VOID)
{
func2() ;
g += TWO ;
}
#define TWO 2
typedef void VOID ;
extern int g;
VOID func1(VOID) ;
VOID func2(VOID)
{
func1() ;
g -= TWO ;
}
file1.c file2.c file3.c
6. 6
Modulization
Split source code into separate files
– Generally create one ~.h file for each ~.c file
– Content of ~.c
• Functions
• Definition and Declaration used the functions in the file
• #include header files necessary in the file
– Content of ~.h
• Extern definition of global variable accessed in the corresponding
~.c file
• Prototype of function defined in the corresponding ~.c file
• #include header files if necessary
7. 7
Modulization
Split source code into separate files
extern int g VOID func1(VOID) ; VOID func2(VOID) ;
int g;
VOID main(VOID)
{
func1() ;
func2() ;
g *= TWO ;
}
VOID func1(VOID)
{
func2() ;
g += TWO ;
}
VOID func2(VOID)
{
func1() ;
g -= TWO ;
}
file1.h file2.h file3.h
file2.c file3.cfile1.c
#define TWO 2
typedef void VOID ;
def.h
8. Modulization
Split source code into separate files
– Include necessary ~.h files in ~.c and ~.h files
file1.h file2.h file3.h
extern int g
#include “def.h”
VOID func1(VOID) ;
#include “def.h”
VOID func2(VOID) ;
#include “def.h”
#include “file2.h”
#include “file3.h”
int g;
VOID main(VOID)
{
func1() ;
func2() ;
g *= TWO ;
}
#include “def.h”
#include “file1.h”
#include “file3.h”
VOID func1(VOID)
{
func2() ;
g += TWO ;
}
#include “def.h”
#include “file1.h”
#include “file2.h”
VOID func2(VOID)
{
func1() ;
g -= TWO ;
}
def.h
#define TWO 2
typedef void VOID ;
file2.c file3.cfile1.c 8
9. Conditional Compilation
Problem with relation of header files
– Some header files are included more than once
• Cause overlapped definitions
– Solve with conditional compilation!
file1.h file2.h file2.h
file1.c file2.c file3.c
def.h
#define TWO 2
typedef void VOID ;
9
def.h is included twice
“TWO” is defined twice
10. Conditional Compilation
Conditional compilation based on #ifndef, #endif
– Place all of content of header file between #ifndef and #endif
– Insert #define UNIQUE_NAME
10