CS/CE 218 Lecture -*- Outline -*- * separate-compilation ** modularity ------------------------------------------ MODULARITY def: a program is *constructed modularly* if Economic benefits of modularity: Defining modular interfaces allows markets where suppliers can compete to supply module implementations. E.g., ------------------------------------------ ... it is composed from parts with well-specified interfaces and those parts can be replaced by others that satisfy the specified interfaces ... automobiles: batteries, tires, windshield wiper blades, headlights, paint, seats, etc. televisions: remote controls, vcrs/dvrs, speakers computer operating systems: applications (editors, spreadsheets, etc.) utilities (backup apps), hardware (keyboards, mice, etc.) ------------------------------------------ BENEFITS OF MODULARITY IN SOFTWARE Software is modular if it is Benefits: - separate compilation: - separate testing/verification - fault isolation: - suppliers can ------------------------------------------ ... composed from pieces with well-specified interfaces (These are often called modules.) ... compile each module separately, thus only need to compile parts that have changed (more efficient for large programs) ... as unit tests (or verification) can be run for each module separately (easier to understand) ... interfaces make it clear which module is to blame for faults (by checking at interfaces, e.g., with assertions) ... compete and can specialize and focus on one type of module or one capability (better implementations and perhaps less expensive) ** How separate compilation is implemented in C (with gcc) *** compile and link whole programs ------------------------------------------ COMPILATION A C compiler (gcc) turns If myprog.c is a whole program (including main), then: gcc myprog.c produces a.out Normally one uses -o option: gcc myprog.c -o myprog produces myprog (or myprog.exe on Windows) ------------------------------------------ ... C source code into object code (binary machine code) Note: a.out is a historical name, based on assembler (hence the 'a') Note: gcc -O myprog.c produces optimized version of a.out *** separate compilation of individual modules (files) Q: Why would you want to save the results of compiling a single file? so when make a mistake don't have to recompile everything... ------------------------------------------ SEPARATE COMPILATION IN C Use the -c option on gcc gcc -c to_roman.c produces ------------------------------------------ ... file to_roman.o Usually you want a header file (.h) file for each module *** linking ------------------------------------------ LINKING Linking: connects Example: Source files: to_roman.c checknum.c roman.c to_roman.h checknum.h Compiler command: gcc -c to_roman.c checknum.c roman.c produces: to_roman.o checknum.o roman.o Link via command: gcc to_roman.o checknum.o roman.o -o roman ------------------------------------------ ... separately compiled files into a single executable program (an app) "Connecting" means putting the address of a separately compiled function into the instruction where the function is called. Notes: you don't pass the .h files directly to the compiler, they are #include'd in the .c files *** libraries ------------------------------------------ LIBRARIES def: a library is Example: the standard C library contains the functions defined in many headers: stdlib.h, stdio.h, string.h, math.h, ctype.h, errno.h float.h, limits.h, locale.h ... Example: the C math library contains: ------------------------------------------ ... a set of functions and data which can be used in other programs ... sqrt, exp, cos, acos, cosh, acosh, sin, tan, log, log10, pow, etc. Note: with gcc, the C standard libraries (including math) are automatically linked in