C Module System C and Data Structures Baojian
C Module System C and Data Structures Baojian Hua bjhua@ustc. edu. cn
Software System is 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 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) We show, in this slide, 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 function 1 … functionm filen function 1 … functionn
General Process // Take MS cl as example, the process from source // files (. c. h) to executables (. exe): 1. c 1. i 1. obj 2. c 2. i 2. obj … … … n. c n. i n. obj preprocessing Compiling libraries a. exe linking
Example // area. h #ifndef AREA_H #define AREA_H double area (int r); #endif // area. c #include “area. h” double area (int r) { double pi = 3. 14; double f = pi *r *r; return f; } // main. c #include “area. h” …
Preprocessing n Take source files (. c. h), generate intermediate files (. i) n n n file inclusion Macro substitution comments removal … afterwards, no header file needed any more See demo …
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 …
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. obj call area call printf
Linking // Resolve external references: // area. obj area: // main. obj call area call printf … // printf. obj printf: …
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: cl) 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 n stdio. h Our goal is to examine the printf function: int printf (const char *format, …);
General Strategy User program call the library function printf () internally makes operating system call to do the real work n n n details vary on different OS OS calls hardware driver n user program libraries: printf() OS routine Offered by hardware company 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 on which, libraries are built 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: 17