modules make les editors git modules make les editors git
play

Modules, Makeles, Editors, Git Modules, Makeles, Editors, Git - PowerPoint PPT Presentation

Modules, Makeles, Editors, Git Modules, Makeles, Editors, Git Creating large programs Creating large programs K08 A large program might contain


  1. Modules, Make�les, Editors, Git Modules, Make�les, Editors, Git Creating large programs Creating large programs K08 Δομές Δεδομένων και Τεχνικές Προγραμματισμού • A large program might contain hundreds of thousands lines of code Κώστας Χατζηκοκολάκης • Having such a program is a single .c �le is not practical - Hard to write - Hard to read and understand - Hard to maintain - Slow to compile • We need to split it in semantically related units / / 1 2 Modules Modules Information Hiding Information Hiding • A module (ενότητα) is a collection of related data and operations • A notion closely related to abstraction • • Since the user does not need to know how the module is implemented, They allow to achieve abstraction (αφαίρεση), a notion of fundamental importance in programming anything not necessary for using the module should be hidden - internal data, auxiliary functions, data types, etc • The user of the module only needs to know what the module does • This allows to modify parts of the program independently • Only the author of the module needs to know how it is implemented - a function visible only within the module cannot a�ect other parts of - This is useful even when the author and the user are the same person the program • They will be used to implement Abstract Data Types later in this course - think of changing a car's tires, it should not a�ect its engine! / / 3 4

  2. Modules in C Modules in C Modules in C Modules in C • A module in C is represented by a header �le module.h • Eg. A stats.h module with two functions - we already know several modules: stdio.h , string.h , … // stats.h - Απλά στατιστικά στοιχεία για πίνακες • It simply declares a list of functions #include <limits.h> // INT_MIN, INT_MAX - also constants and typedefs // Επιστρέφει το μικρότερο στοιχείο του array (ΙΝΤ_ΜΑΧ αν size == 0) int stats_find_min(int array[], int size); • Describes what the module does // Επιστρέφει το μεγαλύτερο στοιχείο του array (ΙΝΤ_MIN αν size == 0) - often with documentation for these functions int stats_find_max(int array[], int size); • Pre�xing all functions with stats_ is a good practice (why?) / / 5 6 Using a C module Using a C module Implementing a C module Implementing a C module • • The module's implementation is provided in a �le module.c #include "module.h" • Use the provided functions • module.c contains the de�nitions of all functions declared in module.h • As users , we don't need to know how the module is implemented! // stats.c - Υλοποίηση του stats module #include "stats.h" // minmax.c - Το βασικό αρχείο του προγράμματος int stats_find_min(int array[], int size) { #include <stdio.h> int min = INT_MAX; // "default" τιμή, μεγαλύτερη από όλε #include "stats.h" for (int i = 0; i < size; i++) int main() { if (array[i] < min) int array[] = { 4, 35, -2, 1 }; min = array[i]; // βρέθηκε νέο ελάχιστο printf("min: %d \n ", stats_find_min(array, 4)); return min; printf("max: %d \n ", stats_find_max(array, 4)); } } / / 7 8

  3. Compiling a program with modules Compiling a program with modules Separate compilation Separate compilation • Simply compiling minmax.c together with module.c works • We can compile each .c �le separately to create an .o �le • Then link all .o �les together to create the executable gcc minmax.c stats.c -o minmax • But this compiles both �les every time gcc -c minmax.c -o minmax.o gcc -c stats.c -o stats.o • What if we change a single �le in a program with 1000 .c �les? gcc minmax.o stats.o -o minmax • If we change minmax.c , we only need to recompile that �le and relink - Makefile s make this very easy / / 9 10 Multiple implementations of a module Multiple implementations of a module Compiling with multiple implementations Compiling with multiple implementations • The same module.h can be implemented in di�erent ways • minmax.c is compiled without knowing how stats.h is implemented - this is abstraction ! // stats_alt.c - Εναλλακτική υλοποίηση του stats module • We can then link with any implementation we want #include "stats.h" // Επιστρέφει true αν value <= array[i] για κάθε i gcc -c minmax.c -o minmax.o int smaller_than_all(int value, int array[], int size) { for (int i = 0; i < size; i++) # use the first implementation if (value > array[i]) gcc -c stats.c -o stats.o return 0; gcc minmax.o stats.o -o minmax int stats_find_min(int array[], int size) { # OR the second for (int i = 0; i < size; i++) gcc -c stats_alt.c -o stats_alt.o if (smaller_than_all(array[i], array, size)) gcc minmax.o stats_alt.o -o minmax return array[i]; return INT_MAX; // εδώ φτάνουμε μόνο σε περίπτωση κενού array } / / 11 12

  4. Multiple implementations of a module Multiple implementations of a module Make�les Make�les • All implementations should provide the same high-level behavior • Good programmers are lazy - So the program will work with any of them - they want to spend their time programming, not compiling • But one implementation might be more e�cient than some other • Nobody likes typing the same gcc ... commands 100 times - This often depends on the speci�c application • We can automate compilation with a Makefile • Which implementation of stats.h would you choose? / / 13 14 A simple Make�le A simple Make�le A simple Make�le - �rst problem A simple Make�le - �rst problem • We modify minmax.c , but make refuses to rebuild minmax # Ενα απλό Makefile (με αρκετά προβλήματα) # Προσοχή στα tabs! minmax: $ make minmax gcc -c minmax.c -o minmax.o make: 'minmax' is up to date. gcc -c stats.c -o stats.o gcc minmax.o stats.o -o minmax • solution: dependencies • This means: to create the �le minmax run these commands minmax: minmax.c stats.c • To compile we run make minmax gcc -c minmax.c -o minmax.o gcc -c stats.c -o stats.o - or simply make to compile the �rst target in the Makefile gcc minmax.o stats.o -o minmax • this means: minmax depends on minmax.c , stats.c - if any of these �les is newer (last modi�cation time) than minmax itself, the commands are run again! / / 15 16

  5. A simple Make�le - second problem A simple Make�le - second problem Implicit rules Implicit rules • We modify minmax.c , but make recompiles everything • make knows how to make foo.o if a �le foo.c exists, by running • Solution: separate rules for each �le we create gcc -c foo.c -o foo.o • This is called an implicit rule minmax.o: minmax.c gcc -c minmax.c -o minmax.o • So we don't need rules for .o �les! stats.o: stats.c gcc -c stats.c -o stats.o minmax: minmax.o stats.o minmax: minmax.o stats.o gcc minmax.o stats.o -o minmax gcc minmax.o stats.o -o minmax • To build minmax we need to build minmax.o , stats.o - minmax.o depends on minmax.c which is newer, so make recompiles - stats.o depends on stats.c which is older, so no need to recompile / / 17 18 Variables Variables CFLAGS variable CFLAGS variable • We can use variables to further simplify the Makefile • A special variable - To create a variable: VAR = ... • Passed as arguments to the compiler when compiling a .o �le using an - To use a variable we write $(VAR) anywhere in the Makefile implicit rule • This allows to easily reuse the Makefile • Eg. enable all warnings, treat them as errors, and allow debugging # Αρχεία .o (αλλάζουμε απλά σε stats_alt.o για τη δεύτερη υλοποίηση!) CFLAGS = -Wall -Werror -g OBJS = minmax.o stats.o # Το εκτελέσιμο πρόγραμμα EXEC = minmax $(EXEC): $( OBJS ) gcc $( OBJS ) -o $( EXEC ) / / 19 20

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