CSC161 2010F, Class 23: Building Libraries Overview: * Why We Need Code Libraries. * Simple Code Libraries in C. * Lab. Admin: * Cookies and napkins * For tomorrow: Read Unit Testing 101 for Non-Programmers. * Links on outline and Web site (I hope) * Remember: You can vote on campus today! * Grading has now dropped to the bottom of my todo list. Sorry. Libraries: * Idea: We often use the same code in multiple programs. * If you design code well, it *should* be usable in multiple programs.a * How do we get the same code used in multiple programs? * Cut and paste. * Easy. * But ... * Can be a lot of effort to cut and paste. * Might want to encapsulate: If the code relies on certain "global" variables, different functions may have overlapping globals. * Write a program to do the cutting and pasting for you. * Same problems as above * Typical solution: Build a "library" - a collection of encapsulated code that you can join with your own code. In C: * Separate files for separate basic components * Can compile together with cc -o program file1.c file2.c file3.c * This wastes time recompiling if you only change one file * So most programmers build OBJECT files (mostly-compiled code) first. cc -c file1.c creates file1.o make file1.o cc -o program file1.o file2.o file3.o Make helps! * You don't need rules for the .o files, Make knows them already * You do need a rule for building the program. Rules look like this TARGET: DEPENDENCIES INSTRUCTIONS program: file1.o file2.o file3.o cc -o program file1.o file2.o file3.o