Compiling and Linking C code Assembly C Source C Source C Source - - PDF document

compiling and linking c code
SMART_READER_LITE
LIVE PREVIEW

Compiling and Linking C code Assembly C Source C Source C Source - - PDF document

Advanced Programming Modular Programming in C Modular Programming Intro n It is not possible to create complex programs using a single source file: n Compiling is slow n Difficult reuse of functions n Impossible collaboration among


slide-1
SLIDE 1

Advanced Programming Modular Programming in C 1

Modular Programming

2

Intro

n It is not possible to create complex programs

using a single source file:

n Compiling is slow n Difficult reuse of functions n Impossible collaboration among different developers n Hard to keep track of modifications

n Programs are usually distributed on many

source files.

slide-2
SLIDE 2

Advanced Programming Modular Programming in C 2

3

Compiling and Linking C code

C Source Code Library Compiler Compiler Compiler C Source Code C Source Code Object Code Object Code Object Code Linker Library Executable Code .c .obj .lib .exe

Assembler

Assembly Source Code

Object Code

4

Compiler Structure

Source code C Optimization Binary code generation Optimization HW-independent code generation Semantic Analysis Syntax Analysis Lexical Analysis Object code

slide-3
SLIDE 3

Advanced Programming Modular Programming in C 3

5

Object code module

n It contains binary code corrsponding to a

source C module

n It contains external references to functions

and variables declared in other modules

n It contains functions and variables used by

  • ther modules

n Only one module contains function main().

6

Object Module

sort.obj sort.c strcmp strcpy stdout inssort cntsort swap Functions used by the module but defined elsewhere Variables used by the module but defined elsewhere Functions defined in the module and used from other modules Variables defined in module and used from other modules Binary Code

slide-4
SLIDE 4

Advanced Programming Modular Programming in C 4

7

Linker

Object Code Object Code Object Code Object Code Object Code Executable Code Binary code

8

Modularity

Each module in C:

n Defines some “private” functions n Defines some “public” functions n Defines some “private” variables n Defines some “public” variables n Calls some “extern” functions n Uses some “extern” variables

slide-5
SLIDE 5

Advanced Programming Modular Programming in C 5

APA-extern 9

Function call (I)

extern void InsertionSort(int A[], int n) ; /* … */ main() { int v[10] ; /* … */ InsertionSort(v, 10) ; /* … */ void InsertionSort(int A[], int n) ; void InsertionSort(int A[], int n) { int i, j, key ; /* … */

10

Function Call (II)

n All functions in a module (file) are public by

default

n Module using an external function must

declare its prototype (with keyword extern)

n Linker will realize the link between modules.

slide-6
SLIDE 6

Advanced Programming Modular Programming in C 6

11

Private functions

n The keyword static is used to specify which

functions must not be public, so they are just usable within the module.

void InsertionSort(int A[], int n) ; void DeleteVector(int A[], int n) ; void InsertionSort(int A[], int n) { . . . } static void DeleteVector(int A[], int n) { . . . }

12

Global Variables

extern int n_elem ; extern double vett[MAX] ; /* … */ { for(i=0; i<n_elem; ++i) vett[i]++ ; int n_elem ; double vett[MAX] ; static int temp ;

slide-7
SLIDE 7

Advanced Programming Modular Programming in C 7

13

Global Variables

n A global variable in a module is visible by all

the other modules by default.

n Module using an external variable must

declare its prototype (with keyword extern)

n The keyword static is used to specify which

variables must not be public, so they are just visible within the module.

n Local variables (within functions) cannot be

shared.

14

Problems

n The function declaration must be exactly the

same on both modules!

extern int f(int x); int f( double x ) { . . . y = f ( z ) ; . . . } Error !

slide-8
SLIDE 8

Advanced Programming Modular Programming in C 8

15

Header files

n A header file (file .h) is used to collect

definitions of “extern” functions and variables

n The Developer of module M exporting

functions/variables must create a header file

n All other modules using module M have to

include the header of M (#include).

16

Example of header file

#include "sort.h” /* used to check consistency */ int n_elem ; double vet[MAX] ; static int temp ; void InsertionSort(int A[], int n) { . . . } static void CancelVector(int A[], int n) ; { . . . } extern void InsertionSort(int A[], int n); extern int n_elem ; extern double vet[MAX] ; sort.h sort.c

slide-9
SLIDE 9

Advanced Programming Modular Programming in C 9

17

Example of header file (II)

#include "sort.h" /* import declarations */ main() { int v[10] ; /* … */ InsertionSort(v, 10) ; /* … */ for(i=0; i<n_elem; ++i) vett[i]++ ; extern void InsertionSort(int A[], int n); extern int n_elem ; extern double vett[MAX] ; sort.h

  • ther.c

18

Notes

n Each module usually exports some functions/

variables and it imports some others

n It is best practice to include in each module its

  • wn header file

n It is better to limit the number of shared

variables

n Also include #define in header files n Groups of logically related modules can share a

single header file

slide-10
SLIDE 10

Advanced Programming Modular Programming in C 10

19

Module Source Compiler Compiler User Source Object Code Object Code Linker .c .obj Module Header .h

#include #include

20

Module Source Compiler Compiler User Source Object Code Object Code Linker .c .obj Module Header .h

#include #include

/* stack.h */ typedef struct stack_tag { … } stack; … push(…); declaration

slide-11
SLIDE 11

Advanced Programming Modular Programming in C 11

21

Module Source Compiler Compiler User Source Object Code Object Code Linker .c .obj Module Header .h

#include

/* stack.c */ #include <stack.h> … push(…){ … } definition

#include

22

Module Source Compiler Compiler User Source Object Code Object Code Linker .c .obj Module Header .h

#include

/* main.c */ #include <stack.h> … int main(){ … push(…); … } usage

#include

slide-12
SLIDE 12

Advanced Programming Modular Programming in C 12

#include

23

Module Source Compiler Compiler User Source Object Code Object Code Linker .c .obj Module Header .h

#include

_push stack.o provided symbol

#include

24

Module Source Compiler Compiler User Source Object Code Object Code Linker .c .obj Module Header .h

#include

_push main.o _push stack.o required symbol

slide-13
SLIDE 13

Advanced Programming Modular Programming in C 13

#include

25

Module Source Compiler Compiler User Source Object Code Object Code Linker .c .obj Module Header .h

#include

_push main.o _push stack.o linking

26

Header guards

n With a large number of modules it is possible to

have multiple inclusions of the same header file

n Double declarations of srtucts n Double declarations of types n Recursive inclusions

/** header.h **/ #ifndef HEADER_H #define HEADER_H /* .. */ #endif

slide-14
SLIDE 14

Advanced Programming Modular Programming in C 14

27

Instantiable ADTs

n The simple implementations of ADT are based

  • n a set of global variables

n This is a singleton ADT solution. n Not suitable when several instances of the

same ADT are required, i.e. when an instantiable ADT is need

n The implementation needs to be adapted to

allow for multiple instances

28

Instantiable ADTs

n A singleton implementation of ADT can be

made instantiable by:

n Moving the variable used by the implementation into

a struct

n Adding a parameter to all ADT functions that specify

  • n which instance it has to operate (a pointer to

that struct)

n Adding an initialization function to initialize the

struct’s fields

slide-15
SLIDE 15

Advanced Programming Modular Programming in C 15

Singleton ADT

29

/* stack.c */ #include <stack.h> static int stack[MAX]; static int top; push(int e){ … } /* stack.h */ #include <stack.h> push(int );

Instantiable ADT

30

/* stack.c */ #include <stack.h> static int stack[MAX]; static int top; push(int e){ stack[++top] = e } /* stack.h */ #include <stack.h> push(int ); /* stack.h */ #include <stack.h> typedef struct ss { int stack[MAX]; int top; } stack; void init(stack*); void push(stack*,int ); /* stack.c */ #include <stack.h> void push(stack*s,int e){ s->stack[++s->top] = e; }

slide-16
SLIDE 16

Advanced Programming Modular Programming in C 16

Singleton vs. Instantiable

31

/* stack.c */ #include <stack.h> static int stack[MAX]; static int top; push(int e){ stack[++top] = e } /* stack.h */ #include <stack.h> push(int ); /* stack.h */ #include <stack.h> typedef struct ss { int stack[MAX]; int top; } stack; void init(stack*); void push(stack*,int ); /* stack.c */ #include <stack.h> void push(stack*s,int e){ s->stack[++s->top] = e; }

Singleton vs. Instantiable

32

/* stack.c */ #include <stack.h> int stack[MAX]; int top; push(int e){ stack[++top] = e } /* stack.h */ #include <stack.h> push(int ); /* stack.h */ #include <stack.h> typedef struct ss { int stack[MAX]; int top; } stack; void init(stack*); void push(stack*, int ); /* stack.c */ #include <stack.h> void push(stack*s,int e){ s->stack[++s->top] = e; } The same data but in a struct that can be instantiated multiple times

slide-17
SLIDE 17

Advanced Programming Modular Programming in C 17

/* stack.c */ #include <stack.h> int stack[MAX]; int top; push(int e){ stack[++top] = e } /* stack.h */ #include <stack.h> push(int ); /* stack.h */ #include <stack.h> typedef struct ss { int stack[MAX]; int top; } stack; void init(stack*); void push(stack*, int ); /* stack.c */ #include <stack.h> void push(stack*s,int e){ s->stack[++s->top] = e; }

Singleton vs. Instantiable

33

The function must know

  • n which instance it has

to operate on The function must know

  • n which instance it has

to operate on