Presentation is loading. Please wait.

Presentation is loading. Please wait.

Makefiles and Notes on Programming Assignment PA2

Similar presentations


Presentation on theme: "Makefiles and Notes on Programming Assignment PA2"— Presentation transcript:

1 Makefiles and Notes on Programming Assignment PA2
Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2010 Programming Assignment PA #2

2 Programming Assignment PA #2
Game of Life Read input line to determine size of board, number of generations, etc. Dynamically allocate memory for three boards of the appropriate size Initialize one board to initial configuration (in center) Play up to n generations After each generation, test for termination — I.e., same as previous or 2nd previous generation Print final generation Optional pause and/or print after each generation for debugging purposes Free dynamically allocated memory! CS-2303, A-Term 2010 Programming Assignment PA #2

3 Dynamically Allocated Arrays
Each board is a 2D array Size determined at run-time gcc feature of dynamic automatic arrays not appropriate for this assignment Allocate array of pointers to rows Allocate array(s) for actual rows CS-2303, A-Term 2010 Programming Assignment PA #2

4 Programming Assignment PA #2
From this point on, every programming assignment at WPI must be accompanied by makefile or equivalent Unless otherwise specified Eclipse, Visual Studio, etc. provide own makefile equivalents Program Organization See Lab2 Must have At least one .h file (your interface) At least two .c files (together your program) One makefile Suggested partition Life.c — main program, read input & initialize Game.c — playing game, testing termination Board.c — utility functions for printing boards, comparison, etc. Life.h — interface with function headers, extern variable for any globals, etc. You may choose your own partition of the problem CS-2303, A-Term 2010 Programming Assignment PA #2

5 Programming Assignment PA #2
Makefile A scripting “language” that specifies how an application is to be built Flags, compiler switches, libraries to link Creates final target and all intermediate files Recompiles only what needs to be E.g., source files that have changed Dependencies upon other files For PA2, target is the program Life CS-2303, A-Term 2010 Programming Assignment PA #2

6 Programming Assignment PA #2
Format of Makefile List of targets Followed by colon and dependencies For each target, command line(s) for building target If any List of environment variables For passing switches to commands, etc. CS-2303, A-Term 2010 Programming Assignment PA #2

7 Programming Assignment PA #2
make command make Builds the first target in file makefile make –f myMakefile Builds the first target in file myMakefile make Life Builds target Life in file makefile make CFLAGS=-g Builds first target using compiler flag –g make CFLAGS=-O2 Life Builds target Life using compiler flag –O2 (i.e., optimized code) CS-2303, A-Term 2010 Programming Assignment PA #2

8 Programming Assignment PA #2
Example Makefile Environment variable (note '=‘ sign) CFLAGS = -g all: Board.o Game.o Life.o Input.o gcc $(CFLAGS) Board.o Game.o Life.o Input.o -o Life *.o: *.c gcc -Wall -c $(CFLAGS) *.c clean: rm -f *.o Life echo "Make clean completed." Board.o: Life.h Game.o: Life.h Target (note colon; note also dependencies following colon ) Command lines must be preceded by tabs, not spaces Command line to build target all Generic target and command for all .o files Note dependency on corresponding .c file Very important target — cleans up the directory (note two commands) Targets with no commands, only dependencies CS-2303, A-Term 2010 Programming Assignment PA #2

9 Summary – make & makefiles
make — a useful tool for building all but the most trivial programs Avoids stupid mistakes in compile commands Builds only the parts of an application that need to be rebuilt Can be combined to make very elaborate build “scripts” CS-2303, A-Term 2010 Programming Assignment PA #2

10 Programming Assignment PA #2
Questions? Next Topic CS-2303, A-Term 2010 Programming Assignment PA #2


Download ppt "Makefiles and Notes on Programming Assignment PA2"

Similar presentations


Ads by Google