c module system

31
C Module System C and Data Structures Baojian Hua [email protected]

Upload: chi

Post on 22-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

C Module System. C and Data Structures Baojian Hua [email protected]. Software Systems are 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: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: C Module System

C Module System

C and Data StructuresBaojian Hua

[email protected]

Page 2: C Module System

Software Systems are 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)

Page 3: C Module System

Module System

Module systems offer a systematic method to organize software Different parts can be separately

developed, compiled, tested, debugged Different parts are finally linked

together This process evolves compiling,

linking and libraries, etc.

Page 4: C Module System

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)

This slide shows how to manage C’s module system: source programs (separate) compiling, linking, loading and tools

Page 5: C Module System

Typical C Program Organization

Program

file1

function1 functionm ……

… filen

function1 functionn

dec & stm

exp

dec & stm

exp

dec & stm

exp

dec & stm

exp

Page 6: C Module System

General Process// General process from source files (.c) to

// executables (.exe):

1.c

2.c

n.c

1.i

2.i

n.i

1.o

2.o

n.o

libraries

a.exe

preprocessing

Compiling

linking

… … …

Page 7: C Module System

Example Revisiteddouble area (int r);

double area (int r){ double pi = 3.14; return (pi*r*r);}

int main(){ double f; f = area(5); return 0;}

Page 8: C Module System

First Try// main.c

int main (){ double f; f = area (5); return 0;}

// area.cdouble area (int r);

#define PI 3.14double 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);

Page 9: C Module System

Second Try// main.cdouble area (int r);

int main (){ double f; f = area (5); return 0;}

// area.cdouble area (int r);

#define PI 3.14double 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?

Page 10: C Module System

Third Try

// area.hdouble area (int r);

// main.c#include “area.h”

int main () …

// area.c#include “area.h”

#define PI 3.14double area (int r){ double f = PI *r *r; return f;}

Try this demo!

Is this perfect!?

Page 11: C Module System

Pitfalls

// area.hdouble area (int r);int i = 0;

// main.c#include “area.h”

int main () …

// area.c#include “area.h”

#define PI 3.14double area (int r){ double f = PI *r *r; return f;}

Try this demo!

Page 12: C Module System

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.14double area (int r){ double f = PI *r *r; return f;}

Page 13: C Module System

Preprocessing Take source files (.c .h), generate

intermediate files file inclusion Macro substitution comments removal … afterwards, no header file needed any more

So, what’s the role of “.h” files?

Page 14: C Module System

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.14double area (int r){ double f = PI*r*r; return f;}

Page 15: C Module System

Example

// area.h#ifndef AREA_H#define AREA_H

double area (int r);

#endif

// main.c#include “area.h”int main () { area (5);}

// area.cdouble area (int r);

#define PI 3.14double area (int r){ double f = PI*r*r; return f;}

Page 16: C Module System

Example

// area.h#ifndef AREA_H#define AREA_H

double area (int r);

#endif

// main.c#include “area.h”int main () { area (5);}

// area.cdouble area (int r);

#define PI 3.14double area (int r){ double f = 3.14*r*r; return f;}

Page 17: C Module System

Example

// area.h#ifndef AREA_H#define AREA_H

double area (int r);

#endif

// main.cdouble area(int r);int main () { area (5);}

// area.cdouble area (int r);

double area (int r){ double f = 3.14*r*r; return f;}

Page 18: C Module System

Example

// area.h#ifndef AREA_H#define AREA_H

double area (int r);

#endif

// main.cdouble area(int r);int main () { area (5);}

// area.cdouble area (int r);

double area (int r){ double f = 3.14*r*r; return f;}

Page 19: C Module System

Compiling

Generate binary object files (.obj) object files in assembly or binary may involve several intermediate phases

analysis optimizations …

See demo …

Page 20: C Module System

Example

// main.cdouble area(int r);int main () { area (5);}

// area.cdouble area (int r);

double area (int r){ double f = 3.14*r*r; return f;}

// area.o010100101010010110100101001010010100101010101010

// main.o110101010010100110101001001010010001001001010010

Page 21: C Module System

Linking

Object files often called relocatable they are incomplete

function names, extern variables, etc. Linking the process of linking all objec

t files together resolve reference to external entities

See demo …

Page 22: C Module System

Linking// Object files are incomplete:

// main.o

area(…)printf(…)

Page 23: C Module System

Linking// Resolve external references:

// main.o

call areacall printf

// area.o

area:…

// printf.o

printf:…

Page 24: C Module System

Example

// main.cdouble area(int r);int main () { area (5);}

// area.cdouble area (int r);

double area (int r){ double f = 3.14*r*r; return f;}

// area.o010100101010010110100101001010010100101010101010

// main.o110101010010100110101001001010010001001001010010

// a.exe010101101……………

Page 25: C Module System

Static vs Dynamic Linking Static: all object files must be available and

link together the generated .exe files are complete what’s the pros and cons?

Dynamic: some object files are absent the generated .exe files are incomplete then how these absent object files are reference

d? what’s the pros and cons?

Page 26: C Module System

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: vc)

Same linking technique, but …

Page 27: C Module System

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 study the “printf” function

int printf (const char *format, …);

Page 28: C Module System

General Strategy User program call the library f

unction printf () printf () internally makes oper

ating system call to do the real work details vary on different OS

OS calls hardware driver Offered by hardware company

user program

libraries:printf()

OSroutines

hardwaredriver

Page 29: C Module System

Case Study: Linux vs Windows fwrite ()

user program

write ()int 0x80

sys_write ()Kernel

fwrite ()user program

write ()

NtWriteFile ()int 0x2e

IoWriteFile ()Kernel

Page 30: C Module System

API & SDK Application programming interface (API)

a set of routines that nicely wrap up operating system calls

libraries are built on top of them 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

Page 31: C Module System

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