Makefiles, Unix, and Project 1 Q&A
-
Upload
honorato-avery -
Category
Documents
-
view
35 -
download
4
description
Transcript of Makefiles, Unix, and Project 1 Q&A
![Page 1: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/1.jpg)
Makefiles, Unix, and Project 1 Q&A
15-441: Recitation 2
September 12, 2007
![Page 2: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/2.jpg)
Overview
Today, I will try to teach you three things.You may also learn something about:
• gcc• make and Makefiles• How to use Unix effectively
Project 1 Q&A
![Page 3: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/3.jpg)
D.R.Y.
Rule 1: Don’t Repeat Yourself
![Page 4: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/4.jpg)
D.R.Y.
Rule 1: Don’t Repeat Yourself
(except for emphasis)
![Page 5: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/5.jpg)
Compiling a Program: gcc
Compile a source file into a binary% gcc source.c lib.c –o binary
Compile a source file into a .o file% gcc –c source.c% gcc –c lib.c
Compile .o files into a binary% gcc lib.o source.o –o binary
![Page 6: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/6.jpg)
More advanced gcc
Compile a source file with debugging hooks% gcc –g source.c –o binary% gdb binary
Activate 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 …
![Page 7: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/7.jpg)
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 binary
But…
Don’t Repeat Yourself
![Page 8: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/8.jpg)
Makefiles: 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 binary
Simplify and modularize…
CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binary
![Page 9: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/9.jpg)
Makefiles: Simplify
target: dependency1 dependency2Unix command (start line with a TAB)Unix command…
% gcc lib.o source.o –o binary
binary: lib.o source.o
gcc lib.o source.o –o binary
![Page 10: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/10.jpg)
binary: lib.o source.ogcc -g -Wall lib.o source.o –o binary
lib.o: lib.cgcc -g -Wall –c lib.c -o lib.o
source.o: source.cgcc -g -Wall –c source.c –o source.o
clean:rm *.o *~ binary
![Page 11: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/11.jpg)
binary: lib.o source.ogcc -g -Wall lib.o source.o –o binary
lib.o: lib.cgcc -g -Wall –c lib.c -o lib.o
source.o: source.cgcc -g -Wall –c source.c –o source.o
clean:rm *.o *~ binary
![Page 12: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/12.jpg)
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.o
source.o: source.c$(CC) $(CFLAGS) –c source.c –o source.o
clean:rm *.o *~ $(OUTPUT)
![Page 13: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/13.jpg)
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.o
source.o: source.c$(CC) $(CFLAGS) –c source.c –o source.o
clean:rm *.o *~ $(OUTPUT)
![Page 14: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/14.jpg)
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.o
source.o: source.c$(CC) $(CFLAGS) –c source.c –o source.o
clean:rm *.o *~ $(OUTPUT)
![Page 15: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/15.jpg)
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.o
source.o: source.c$(CC) $(CFLAGS) –c source.c –o source.o
clean:rm *.o *~ $(OUTPUT)
![Page 16: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/16.jpg)
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)
![Page 17: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/17.jpg)
Using a Makefile
% make% make cleanM-x compile
That’s it!
![Page 18: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/18.jpg)
More about Makefiles
Rule 2: Use the Google.
Google: “make tutorial”
Google: “makefile example”
Google: “makefile template”
![Page 19: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/19.jpg)
Simple Scripting
% ./server 6667 &% cat testfile.01 | ./testscript.py% cat testfile.02 | ./testscript.py% killall -9 server
![Page 20: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/20.jpg)
Simple Scripting
#/bin/sh
echo “Starting server on port 6667.”./server 6667 &SERVERPID = $!
echo “Running test files.”cat testfile.01 | ./testscript.pycat testfile.02 | ./testscript.py
echo “Killing server process.”kill $(SERVERPID)
![Page 21: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/21.jpg)
#/bin/sh
TESTFILES=testfile.01 testfile.02 testfile.03
echo “Starting server on port 6667.”./server 6667 &SERVERPID = $!
for testfile in $(TESTFILES); doecho “Running test file ‘$testfile’”cat $testfile | ./testscript.py
fi
echo “Killing server process.”kill $(SERVERPID)
![Page 22: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/22.jpg)
CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binaryOBJFILES = lib.o source.o
all: $(OUTPUT)
$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) –o $(OUTPUT)
%.o: %.c$(CC) $(CFLAGS) –c %< -o %@
clean:rm *.o *~ $(OUTPUT)
![Page 23: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/23.jpg)
CC = gccCFLAGS = -g –Wall –WerrorOUTPUT = binaryOBJFILES = lib.o source.o
all: $(OUTPUT) test
$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) –o $(OUTPUT)
%.o: %.c$(CC) $(CFLAGS) –c %@ -o %<
test: $(OUTPUT)sh ./testscript.sh
clean:rm *.o *~ $(OUTPUT)
![Page 24: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/24.jpg)
Unix
Debugging: gdb, valgrind
Scripting: Perl, Python, Ruby
We will cover these in later recitations…
Editors: vi, emacs, ed, whatever
Tools: sed, awk, grep, find, wget, tar
Installing Unix packages
![Page 25: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/25.jpg)
Unix
Unix Philosophy:
Do one thing, and do it well.
Rule 3:
Learn one thing at a time, and learn it well.
![Page 26: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/26.jpg)
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.new
![Page 27: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/27.jpg)
How 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.gz
![Page 28: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/28.jpg)
How 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 install
![Page 29: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/29.jpg)
In General…
Use the keyboard.
(Touching the mouse is a cache miss.)
![Page 30: Makefiles, Unix, and Project 1 Q&A](https://reader036.fdocuments.in/reader036/viewer/2022062721/5681364c550346895d9dcd72/html5/thumbnails/30.jpg)
Project 1
Checkpoint 1: Any problems?
Checkpoint 2: Be ready for it soon!
Due date: It will come faster than you think.
Questions?