Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Makefiles, Unix, and Project 1 Q&A15-441: Recitation 2September 12, 2007OverviewToday, I will try to teach you three things.You may also learn something about:•gcc•make and Makefiles•How to use Unix effectivelyProject 1 Q&AD.R.Y.Rule 1: Don’t Repeat YourselfD.R.Y.Rule 1: Don’t Repeat Yourself(except for emphasis)Compiling a Program: gccCompile a source file into a binary% gcc source.c lib.c –o binaryCompile a source file into a .o file% gcc –c source.c% gcc –c lib.cCompile .o files into a binary% gcc lib.o source.o –o binaryMore advanced gccCompile a source file with debugging hooks% gcc –g source.c –o binary% gdb binaryActivate all warnings, treat warnings as errors% gcc –Wall –Werror …Compile with optimizations; speeds up binary% gcc –O2 …Set a predefined macro (same as #define DEBUG)% gcc –DDEBUG …Use cases% gcc –g –Wall –Werror –c source.c –o source.o% gcc –g –Wall –Werror –c lib.c –o lib.o% gcc –g –Wall –Werror lib.o source.o –o binaryBut…Don’t Repeat YourselfMakefiles: Simplify% gcc –g –Wall –Werror –c source.c –o source.o% gcc –g –Wall –Werror –c lib.c –o lib.o% gcc –g –Wall –Werror lib.o source.o –o binarySimplify and modularize…CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binaryMakefiles: Simplifytarget: dependency1 dependency2Unix command (start line with a TAB)Unix command…% gcc lib.o source.o –o binarybinary: lib.o source.ogcc lib.o source.o –o binarybinary: lib.o source.ogcc -g -Wall lib.o source.o –o binarylib.o: lib.cgcc -g -Wall –c lib.c -o lib.osource.o: source.cgcc -g -Wall –c source.c –o source.oclean:rm *.o *~ binarybinary: lib.o source.ogcc -g -Wall lib.o source.o –o binarylib.o: lib.cgcc -g -Wall –c lib.c -o lib.osource.o: source.cgcc -g -Wall –c source.c –o source.oclean:rm *.o *~ binaryCC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binary$(OUTPUT): lib.o source.o$(CC) $(CFLAGS) lib.o source.o –o $(OUTPUT) lib.o: lib.c$(CC) $(CFLAGS) –c lib.c -o lib.osource.o: source.c$(CC) $(CFLAGS) –c source.c –o source.oclean:rm *.o *~ $(OUTPUT)CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binary$(OUTPUT): lib.o source.o$(CC) $(CFLAGS) lib.o source.o –o $(OUTPUT) lib.o: lib.c$(CC) $(CFLAGS) –c lib.c -o lib.osource.o: source.c$(CC) $(CFLAGS) –c source.c –o source.oclean:rm *.o *~ $(OUTPUT)CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binaryOBJFILES = lib.o source.o$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) –o $(OUTPUT) lib.o: lib.c$(CC) $(CFLAGS) –c lib.c -o lib.osource.o: source.c$(CC) $(CFLAGS) –c source.c –o source.oclean:rm *.o *~ $(OUTPUT)CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binaryOBJFILES = lib.o source.o$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) –o $(OUTPUT) lib.o: lib.c$(CC) $(CFLAGS) –c lib.c -o lib.osource.o: source.c$(CC) $(CFLAGS) –c source.c –o source.oclean:rm *.o *~ $(OUTPUT)CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binaryOBJFILES = lib.o source.o$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) –o $(OUTPUT) %.o: %.c# $< = the source: %.c# $@ = the target: %.o$(CC) $(CFLAGS) –c $< -o $@clean:rm *.o *~ $(OUTPUT)Using a Makefile% make% make cleanM-x compileThat’s it!More about MakefilesRule 2: Use the Google.Google: “make tutorial”Google: “makefile example”Google: “makefile template”Simple Scripting% ./server 6667 &% cat testfile.01 | ./testscript.py% cat testfile.02 | ./testscript.py% killall -9 serverSimple Scripting#/bin/shecho “Starting server on port 6667.”./server 6667 &SERVERPID = $!echo “Running test files.”cat testfile.01 | ./testscript.pycat testfile.02 | ./testscript.pyecho “Killing server process.”kill $(SERVERPID)#/bin/shTESTFILES=testfile.01 testfile.02 testfile.03echo “Starting server on port 6667.”./server 6667 &SERVERPID = $!for testfile in $(TESTFILES); doecho “Running test file ‘$testfile’”cat $testfile | ./testscript.pyfiecho “Killing server process.”kill $(SERVERPID)CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binaryOBJFILES = lib.o source.oall: $(OUTPUT)$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) –o $(OUTPUT) %.o: %.c$(CC) $(CFLAGS) –c %< -o %@clean:rm *.o *~ $(OUTPUT)CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binaryOBJFILES = lib.o source.oall: $(OUTPUT) test$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) –o $(OUTPUT) %.o: %.c$(CC) $(CFLAGS) –c %@ -o %<test: $(OUTPUT)sh ./testscript.shclean:rm *.o *~ $(OUTPUT)UnixDebugging: gdb, valgrindScripting: Perl, Python, RubyWe will cover these in later recitations…Editors: vi, emacs, ed, whateverTools: sed, awk, grep, find, wget, tarInstalling Unix packagesUnixUnix Philosophy:Do one thing, and do it well.Rule 3:Learn one thing at a time, and learn it well.How do I……find all instances of “func_name”:% grep –r “func_name”…replace all instances of bad_func_name to good_func_name:% sed –e “s/bad_func_name/good_func_name/g”\source.c > source.c.newHow do I……find a file named “source.c”:% find . –name “source.c”…download a .tar.gz file from the Internet:% wget http://address/to/file.tar.gz…untar and unzip the tarball:% tar xzvf file.tar.gzHow do I……install a package from source:% wget http://address/to/file.tar.gz% tar xzvf file.tar.gz% cd file% less README (always important!)% ./configure% make && make installIn General…Use the keyboard.(Touching the mouse is a cache miss.)Project 1Checkpoint 1: Any problems?Checkpoint 2: Be ready for it soon!Due date: It will come faster than you
View Full Document