Transcript slides

C Module System
C and Data Structures
Baojian Hua
[email protected]
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)
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
Typical C Program
Organization
Program
…
file1
function1
…
functionm
filen
function1
…
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

Take source files (.c .h), generate
intermediate files (.i)






file inclusion
Macro substitution
comments removal
…
afterwards, no header file needed any more
See demo …
Compiling

Generate binary object files (.obj)


object files in assembly or binary
may involve several intermediate phases




analysis
optimizations
…
See demo …
Linking

Object files often called relocatable

they are incomplete


Linking the process of linking all object
files together


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?

Libraries just are pre-written pre-compiled
object files

Normally offered by the compiler company


Header files are available



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?

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, …);
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

user program
libraries:
printf()
OS
routine
Offered by hardware company
hardware
driver
Case Study: Linux vs Windows
fwrite ()
user program
fwrite ()
user program
write ()
int 0x80
write ()
NtWriteFile ()
int 0x2e
sys_write ()
Kernel
IoWriteFile ()
Kernel
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
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