compiling and linking c code
play

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


  1. 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 different developers n Hard to keep track of modifications n Programs are usually distributed on many source files. 2 1

  2. Advanced Programming Modular Programming in C Compiling and Linking C code Assembly C Source C Source C Source Source .c Code Code Code Code Compiler Compiler Compiler Assembler Object Object Object Object .obj Code Code Code Code .lib Linker Executable .exe Code Library Library 3 Compiler Structure Source Lexical Analysis code C Syntax Analysis Semantic Analysis HW-independent code generation Optimization Binary code generation Object code Optimization 4 2

  3. Advanced Programming Modular Programming in C 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 other modules n Only one module contains function main(). 5 Object Module sort.c Functions defined in Functions used by the module and used the module but from other modules defined elsewhere sort.obj strcmp inssort strcpy cntsort Binary stdout Code swap Variables used by Variables defined in the module but module and used defined elsewhere from other modules 6 3

  4. Advanced Programming Modular Programming in C Linker Object Object Code Code Object Code Object Object Code Code Executable Code Binary code 7 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 8 4

  5. Advanced Programming Modular Programming in C Function call (I) void InsertionSort(int A[], int n) ; void InsertionSort(int A[], int n) { int i, j, key ; /* … */ extern void InsertionSort(int A[], int n) ; /* … */ main() { int v[10] ; /* … */ InsertionSort(v, 10) ; /* … */ APA-extern 9 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. 10 5

  6. Advanced Programming Modular Programming in C 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) { . . . } 11 Global Variables int n_elem ; double vett[MAX] ; static int temp ; extern int n_elem ; extern double vett[MAX] ; /* … */ { for(i=0; i<n_elem; ++i) vett[i]++ ; 12 6

  7. Advanced Programming Modular Programming in C 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. 13 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 ! } 14 7

  8. Advanced Programming Modular Programming in C 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). 15 Example of header file sort.h extern void InsertionSort(int A[], int n); extern int n_elem ; extern double vet[MAX] ; #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) ; { . . . } sort.c 16 8

  9. Advanced Programming Modular Programming in C Example of header file (II) extern void InsertionSort( int A[], int n ); sort.h extern int n_elem ; extern double vett[MAX] ; other.c #include "sort.h" /* import declarations */ main() { int v[10] ; /* … */ InsertionSort(v, 10) ; /* … */ for(i=0; i<n_elem; ++i) vett[i]++ ; 17 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 own 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 18 9

  10. Advanced Programming Modular Programming in C Module .h Header #include #include Module User .c Source Source Compiler Compiler Object Object .obj Code Code Linker 19 /* stack.h */ typedef struct stack_tag { Module … .h Header #include } stack; … #include push(…); Module User .c Source Source declaration Compiler Compiler Object Object .obj Code Code Linker 20 10

  11. Advanced Programming Modular Programming in C Module .h Header #include #include Module User .c Source Source Compiler Compiler /* stack.c */ #include <stack.h> … Object Object .obj push(…){ Code Code … } definition Linker 21 Module .h Header #include #include Module User .c Source Source /* main.c */ Compiler Compiler #include <stack.h> … Object Object int main(){ .obj Code Code … push(…); … } Linker usage 22 11

  12. Advanced Programming Modular Programming in C stack.o Module _push .h Header #include provided symbol #include Module User .c Source Source Compiler Compiler Object Object .obj Code Code Linker 23 stack.o main.o Module _push _push .h Header #include #include Module User .c Source Source required symbol Compiler Compiler Object Object .obj Code Code Linker 24 12

  13. Advanced Programming Modular Programming in C stack.o main.o Module _push _push .h Header #include #include linking Module User .c Source Source Compiler Compiler Object Object .obj Code Code Linker 25 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 26 13

  14. Advanced Programming Modular Programming in C Instantiable ADTs n The simple implementations of ADT are based on 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 27 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 on which instance it has to operate (a pointer to that struct ) n Adding an initialization function to initialize the struct ’s fields 28 14

  15. Advanced Programming Modular Programming in C Singleton ADT /* stack.h */ #include <stack.h> push(int ); /* stack.c */ #include <stack.h> static int stack[MAX]; static int top; push(int e){ … } 29 Instantiable ADT /* stack.h */ #include <stack.h> /* stack.h */ typedef struct ss { #include <stack.h> int stack[MAX]; push(int ); int top; } stack; void init(stack*); void push(stack*,int ); /* stack.c */ #include <stack.h> /* stack.c */ static int stack[MAX]; #include <stack.h> static int top; void push(stack*s,int e){ s->stack[++s->top] = e; push(int e){ stack[++top] = e } } 30 15

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend