Lecture 14 ----------------- Announce ----------------- - grades! proj2 and hw8 - hw9 due Today ----------------- - proj3/hw9 questions/review - UNIX C/C++ Project Management Review ------------------ UNIX C/C++ Devel ----------------- - so far writing relatively small programs in single program files - when projects get larger, some things happen A.) program files get huge B.) functionality repeated many times, in many programs C.) compiler commands get complex A.) unreasonable to put all code in one file - start putting logical pieces of code in their own files - start thinking about which functions 'fit' together and put them in one place. ex: all functions that setup and perform linked list functionality - use header files for prototypes and user defined data types ex: linked list. - have all linked list functions in one file - put prototypes/data types in a header so any program that needs to use a linked list need only include the header file. - problem with headers, multiple includes per project ex: main.c and ll.c both #inlcude - compiler will complain #ifdef INCLUDE_LL_H #define INCLUDE_LL_H // proto and data #endif B.) much duplicated functionality, need for generic code - goal is to write some piece of code once, use forever ex: linked list again, attempt to write generic data structure so we don't need one for every data type we have struct node_t { void *data; struct node_t *link; }; struct ll_t { struct node_t *head; int size; }; // make a new node struct node_t *newnode = malloc(sizeof(struct node_t)); newnode->data = malloc(sizeof(int)); - once we have this code written, we can create a 'library' and never have to compile the code again. - library is just compiled code that is put together into one library file (*.a *.so) - first create compiled code - gcc -c -I./ linklist.c - output: linklist.o - then put *.o files into a library - ar rs liblinklist.a linklist.o - - we've been using libraries all along, gcc includes some by default like printf/scanf/etc - to link against libraries explicitly, use -l to gcc - gcc -I./ hw8.c -llinklist - -l looks for file named 'lib.a' - take a look at /lib and /usr/lib, there are very many libraries preinstalled on UNIX systems. SO: - we've moved to a 'multiple file' environment - we've created 'libraries' to hold precompiled, generic code - now compiling is really annoying! C.) typing in compiler commands is not feasible - think if we had multiple directories of .c files, some are libraries, some are to become the executable - compiling by hand is a nightmare - UNIX provides a tool called 'make' to help us with large projects - make main objective is to generate object files if the source code has changed - command 'make' reads from a file called 'Makefile' in the current directory - Makefile contains special env variables, shell operations, targets (sections) ex: CFLAGS="-I./" LDFLAGS="-L./ -llinklist" OBJS="project2.o linklist.o card.o" all: $OBJS linklist @echo making all gcc $OBJS -o blackjack $LDFLAGS linklist: @echo making linklist library ar rs linklist.a linklist.o clean: @echo making clean rm -f *.o *.a blackjack - targets specified with 'targetname:' - default runs 'all:' - anything after colon is processed first, then section processed (dependencies) - if target is 'somthing.o' then make automatically calls gcc $CFLAGS -o somthing.c