Download presentation
Presentation is loading. Please wait.
1
C Module System C and Data Structures Baojian Hua bjhua@ustc.edu.cn
2
Software System is Large Practical software systems tend to be large and complex: Linux kernel consists of ~1000K LOC So the general principals in designing large (even small) software systems are: dividing into manageable smaller ones separating specification (interface) from code (implementation)
3
Module System Different styles of module systems in languages: 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: source programs (separate) compiling, linking, loading and tools
4
Typical C Program Organization Program file1 function1functionm … … … filen function1functionn
5
General Process // Take MS cl as example, the process from source // files (.c.h) to executables (.exe): 1.c 2.c n.c 1.i 2.i n.i 1.obj 2.obj n.obj libraries a.exe preprocessingCompiling linking ………
6
Example // area.h #ifndef AREA_H #define AREA_H double area (int r); #endif // main.c #include “area.h” … // area.c #include “area.h” double area (int r) { double pi = 3.14; double f = pi *r *r; return f; }
7
Preprocessing Take source files (.c.h), generate intermediate files (.i) file inclusion Macro substitution comments removal … afterwards, no header file needed any more See demo …
8
Compiling Generate binary object files (.obj) object files in assembly or binary may involve several intermediate phases analysis optimizations … See demo …
9
Linking Object files often called relocatable they are incomplete function names, extern variables, etc. Linking the process of linking all object files together resolve reference to external entities See demo …
10
Linking // Object files are incomplete: // main.obj call area call printf
11
Linking // Resolve external references: // main.obj call area call printf // area.obj area: … // printf.obj printf: …
12
What are Libraries? Libraries just are pre-written pre-compiled object files Normally offered by the compiler company For user program linking purpose Header files are available Ex: stdio.h, stdlib.h, ctype.h, …, from C standard library Source code available (ex: gcc), or unavailable (ex: cl) Same linking technique, but …
13
How to Implement Libraries? In order to familiarize you with libraries implementation techniques and others, we next study carefully an example stdio.h Our goal is to examine the printf function: int printf (const char *format, …);
14
General Strategy User program call the library function printf () printf () internally makes operating system call to do the real work details vary on different OS OS calls hardware driver Offered by hardware company user program libraries: printf() OS routine hardware driver
15
Case Study: Linux vs Windows fwrite () user program write () int 0x80 sys_write () Kernel fwrite () user program write () NtWriteFile () int 0x2e IoWriteFile () Kernel
16
API & SDK Application programming interface (API) a set of routines that nicely wrap up operating system calls on which, libraries are built standard C libraries, runtime, etc. Why API? Software Development Kit (SDK) A collection of APIs header, libraries, files, and tools Varies on different Windows version
17
Runtime Runtime is a special set of libraries For program startup and exit get system info libraries preparation, etc. Normally NOT for called by user program Again, vary between different systems
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.