C Module System C and Data Structures Baojian
C Module System C and Data Structures Baojian Hua bjhua@ustc. edu. cn
Software Systems are Large n Practical software systems tend to be large and complex: n n Linux kernel consists of ~1000 K LOC So the general principals in designing large (even small) software systems are: n n dividing into manageable smaller ones separating specification (interface) from code (implementation)
Module System n Module systems offer a systematic method to organize software n n n Different parts can be separately developed, compiled, tested, debugged Different parts are finally linked together This process evolves compiling, linking and libraries, etc.
Module System n Different styles of module systems in languages: n n ML signature and structure Java interface and class C (C++) header files (. h) and C files (. c) This slide shows how to manage C’s module system: n n n source programs (separate) compiling, linking, loading and tools
Typical C Program Organization Program … file 1 filen functionm function 1 dec & stm exp exp function 1 … … functionn
General Process // General process from source files (. c) to // executables (. exe): 1. c 1. i 1. o 2. c 2. i 2. o … … … n. c n. i n. o preprocessing Compiling libraries a. exe linking
Example Revisited double area (int r); double area (int r) { double pi = 3. 14; return (pi*r*r); } int main() { double f; f = area(5); return 0; }
First Try // main. c int main () { double f; f = area (5); return 0; } // area. c double area (int r); #define PI 3. 14 double area (int r) { double f = PI *r *r; return f; } Try this demo! Wrong result? Why? Or even worse? f = area(5, 6, 7, 8, 9);
Second Try // main. c double area (int r); // area. c double area (int r); int main () { double f; f = area (5); return 0; } #define PI 3. 14 double area (int r) { double f = PI *r *r; return f; } Try this demo! Is this perfect? What about here is 10000 files contains “area” and “area. c” being changed?
Third Try // area. h double area (int r); // area. c #include “area. h” #define PI 3. 14 double area (int r) { double f = PI *r *r; return f; } // main. c #include “area. h” int main () … Try this demo! Is this perfect!?
Pitfalls // area. h double area (int r); int i = 0; // area. c #include “area. h” #define PI 3. 14 double area (int r) { double f = PI *r *r; return f; } // main. c #include “area. h” int main () … Try this demo!
Final Version // area. h #ifndef AREA_H #define AREA_H double area (int r); #endif // main. c #include “area. h” … // area. c #include “area. h” #define PI 3. 14 double area (int r) { double f = PI *r *r; return f; }
Preprocessing n Take source files (. c. h), generate intermediate files n n n file inclusion Macro substitution comments removal … afterwards, no header file needed any more So, what’s the role of “. h” files?
Example // area. h #ifndef AREA_H #define AREA_H double area (int r); #endif // main. c #include “area. h” int main () { area (5); } // area. c #include “area. h” #define PI 3. 14 double area (int r) { double f = PI*r*r; return f; }
Example // area. h #ifndef AREA_H #define AREA_H double area (int r); #endif // area. c double area (int r); #define PI 3. 14 double area (int r) { double f = PI*r*r; return f; // main. c #include “area. h” int main () { area (5); } }
Example // area. h #ifndef AREA_H #define AREA_H double area (int r); #endif // area. c double area (int r); #define PI 3. 14 double area (int r) { double f = 3. 14*r*r; return f; // main. c #include “area. h” int main () { area (5); } }
Example // area. h #ifndef AREA_H #define AREA_H // area. c double area (int r); double area (int r) { double f = 3. 14*r*r; #endif return f; // main. c double area(int r); int main () { area (5); } }
Example // area. h #ifndef AREA_H #define AREA_H // area. c double area (int r); double area (int r) { double f = 3. 14*r*r; #endif return f; // main. c double area(int r); int main () { area (5); } }
Compiling n Generate binary object files (. obj) n n object files in assembly or binary may involve several intermediate phases n n analysis optimizations … See demo …
Example // area. c double area (int r); double area (int r) { double f = 3. 14*r*r; // area. o 010100101001 0100101010 return f; } // main. c double area(int r); int main () { area (5); } // main. o 110100101001 10101001001 00010010010
Linking n Object files often called relocatable n they are incomplete n n Linking the process of linking all object files together n n function names, extern variables, etc. resolve reference to external entities See demo …
Linking // Object files are incomplete: // main. o area(…) printf(…)
Linking // Resolve external references: // area. o area: // main. o call area call printf … // printf. o printf: …
Example // area. c double area (int r); double area (int r) { double f = 3. 14*r*r; return f; } // main. c double area(int r); int main () { area (5); } // area. o 010100101001 0100101010 // a. exe 010101101 …………… // main. o 110100101001 10101001001 00010010010
Static vs Dynamic Linking n Static: all object files must be available and link together n n n the generated. exe files are complete what’s the pros and cons? Dynamic: some object files are absent n n n the generated. exe files are incomplete then how these absent object files are referenced? what’s the pros and cons?
What are Libraries? n Libraries just are pre-written pre-compiled object files n Normally offered by the compiler company n n Header files are available n n n For user program linking purpose Ex: stdio. h, stdlib. h, ctype. h, …, from C standard library Source code available (ex: gcc), or unavailable (ex: vc) Same linking technique, but …
How to Implement Libraries? n In order to familiarize you with libraries implementation techniques and others, we next study carefully an example n stdio. h Our goal is to study the “printf” function n int printf (const char *format, …);
General Strategy n n User program call the library function printf () internally makes operating system call to do the real work n n libraries: printf() details vary on different OS OS calls hardware driver n user program Offered by hardware company OS routines hardware driver
Case Study: Linux vs Windows fwrite () user program write () int 0 x 80 write () Nt. Write. File () int 0 x 2 e sys_write () Kernel Io. Write. File () Kernel
API & SDK n Application programming interface (API) n n a set of routines that nicely wrap up operating system calls libraries are built on top of them n n n standard C libraries, runtime, etc. Why API? Software Development Kit (SDK) n A collection of APIs n n header, libraries, files, and tools Varies on different Windows version
Runtime n Runtime is a special set of libraries n n n For program startup and exit get system info libraries preparation, etc. Normally NOT for called by user program Again, vary between different systems
- Slides: 31