Modulization
Developing a Large Program Developing a large Program– We can split source codes into several ~. h and ~.c files– There a...
3Modulization Split source code into separate files#define TWO 2typedef void VOID ;int g;VOID func1(VOID) ;VOID func2(VOI...
4Modulization Split source code into separate files– Make ~.c files for each function– Add #define, declaration, function...
5Modulization Split source code into separate files – Problem– When you want to modify #define or typedef definitions• Yo...
6Modulization Split source code into separate files– Generally create one ~.h file for each ~.c file– Content of ~.c• Fun...
7Modulization Split source code into separate filesextern int g VOID func1(VOID) ; VOID func2(VOID) ;int g;VOID main(VOID...
Modulization Split source code into separate files– Include necessary ~.h files in ~.c and ~.h filesfile1.h file2.h file3...
Conditional Compilation Problem with relation of header files– Some header files are included more than once• Cause overl...
Conditional Compilation Conditional compilation based on #ifndef, #endif– Place all of content of header file between #if...
11Conditional Compilation#ifndef _FILE1_H_#define _FILE1_H_extern int g#endif#ifndef _FILE2_H_#define _FILE2_H_#include “d...
12#ifndef _FILE1_H_#define _FILE1_H_extern int g#endif#ifndef _FILE2_H_#define _FILE2_H_#include “def.h”VOID func1(VOID) ;...
Upcoming SlideShare
Loading in …5
×

15 3. modulization

384 views
290 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
384
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

15 3. modulization

  1. 1. Modulization
  2. 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 aremodified not all of source codes.2
  3. 3. 3Modulization Split source code into separate files#define TWO 2typedef void VOID ;int g;VOID func1(VOID) ;VOID func2(VOID ) ;VOID main(VOID ){func1() ;func2() ;g *= TWO ;}file.cVOID func1(VOID ){func2() ;g += TWO ;}VOID func2(VOID ){func1() ;g -= TWO ;}To be writtenby friend 1To be writtenby friend 2To be writtenby me
  4. 4. 4Modulization Split source code into separate files– Make ~.c files for each function– Add #define, declaration, function prototypes, extern objectsPrototype of Functions#define TWO 2typedef void VOID ;int g;VOID func1(VOID) ;VOID func2(VOID) ;VOID main(VOID){func1() ;func2() ;g *= TWO ;}#define TWO 2typedef void VOID ;extern int g;VOID func2(VOID) ;VOID func1(VOID){func2() ;g += TWO ;}#define TWO 2typedef void VOID ;extern int g;VOID func1(VOID) ;VOID func2(VOID){func1() ;g -= TWO ;}file1.c file2.c file3.c
  5. 5. 5Modulization 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 2typedef void VOID ;int g;VOID func1(VOID) ;VOID func2(VOID) ;VOID main(VOID){func1() ;func2() ;g *= TWO ;}#define TWO 2typedef void VOID ;extern int g;VOID func2(VOID) ;VOID func1(VOID){func2() ;g += TWO ;}#define TWO 2typedef void VOID ;extern int g;VOID func1(VOID) ;VOID func2(VOID){func1() ;g -= TWO ;}file1.c file2.c file3.c
  6. 6. 6Modulization 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. 7Modulization Split source code into separate filesextern 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.hfile2.c file3.cfile1.c#define TWO 2typedef void VOID ;def.h
  8. 8. Modulization Split source code into separate files– Include necessary ~.h files in ~.c and ~.h filesfile1.h file2.h file3.hextern 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 2typedef void VOID ;file2.c file3.cfile1.c 8
  9. 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.hfile1.c file2.c file3.cdef.h#define TWO 2typedef void VOID ;9def.h is included twice“TWO” is defined twice
  10. 10. Conditional Compilation Conditional compilation based on #ifndef, #endif– Place all of content of header file between #ifndef and #endif– Insert #define UNIQUE_NAME10
  11. 11. 11Conditional Compilation#ifndef _FILE1_H_#define _FILE1_H_extern int g#endif#ifndef _FILE2_H_#define _FILE2_H_#include “def.h”VOID func1(VOID) ;#endif#ifndef _FILE3_H_#defien _FILE3_H_#include “def.h”VOID func2(VOID) ;#endif#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 ;}#ifndef _DEF_H_#define _DEF_H_#define TWO 2typedef void VOID ;#endiffile2.c file3.cfile1.cfile1.h file2.h file3.hdef.h
  12. 12. 12#ifndef _FILE1_H_#define _FILE1_H_extern int g#endif#ifndef _FILE2_H_#define _FILE2_H_#include “def.h”VOID func1(VOID) ;#endif#ifndef _FILE3_H_#defien _FILE3_H_#include “def.h”VOID func2(VOID) ;#endif#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”static int g1 ;VOID func1(VOID){func2() ;g += TWO ;}static VOID sfunc1(VOID){}#include “def.h”#include “file1.h”#include “file2.h”static int g2 ;VOID func2(VOID){func1() ;g -= TWO ;}static VOID sfunc2(VOID){}#ifndef _DEF_H_#define _DEF_H_#define TWO 2typedef void VOID ;#endiffile2.c file3.cfile1.cfile1.h file2.h file3.hdef.h

×