makefiles & project 1 q&a
DESCRIPTION
Makefiles & Project 1 Q&A. 15-441 Recitation 2 441 Staff. Outline. gcc make and Makefile Useful commands Project 1 Q&A. Simple gcc. If we have files: • prog.c: The main program file • lib.c: Library .c file • lib.h: Library header file % gcc -c prog.c -o prog.o - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/1.jpg)
Makefiles & Project 1 Q&A
15-441 Recitation 2441 Staff
![Page 2: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/2.jpg)
Outline
• gcc• make and Makefile• Useful commands• Project 1 Q&A
![Page 3: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/3.jpg)
Simple gcc
If we have files:• prog.c: The main program file• lib.c: Library .c file• lib.h: Library header file
% gcc -c prog.c -o prog.o% gcc -c lib.c -o lib.o% gcc lib.o prog.o -o binary
![Page 4: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/4.jpg)
gcc flags
• Useful flags1. -g: debugging hook2. -Wall: all warning3. -Werror: treat warning as errors4. -O2, -O3: optimization5. -DDEBUG: macro for DEBUG (#define DEBUG)
![Page 5: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/5.jpg)
Examples
% gcc -g -Wall -Werror -c prog.c -o prog.o% gcc -g -Wall -Werror -c lib.c -o lib.o% gcc -g -Wall -Werror lib.o prog.o -o binary
But Don’t Repeat Yourself!
![Page 6: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/6.jpg)
Makefile
% gcc -g -Wall -Werror -c prog.c -o prog.o% gcc -g -Wall -Werror -c lib.c -o lib.o% gcc -g -Wall -Werror lib.o prog.o -o binary
CC = gccCFLAGS = -g -Wall -WerrorOUTPUT = binary
![Page 7: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/7.jpg)
Makefile
target: dependency1 dependency2 ...unix command (start line with TAB)unix command...
% gcc lib.o prog.o -o binary
binary: lib.o prog.ogcc lib.o prog.o -o binary
![Page 8: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/8.jpg)
binary: lib.o prog.ogcc -g -Wall lib.o prog.o -o binary
lib.o: lib.cgcc -g -Wall -c lib.c -o lib.o
prog.o: prog.cgcc -g -Wall -c prog.c -o prog.o
clean:rm *.o binary
![Page 9: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/9.jpg)
binary: lib.o prog.ogcc -g -Wall lib.o prog.o -o binary
lib.o: lib.cgcc -g -Wall -c lib.c -o lib.o
prog.o: prog.cgcc -g -Wall -c prog.c -o prog.o
clean:rm *.o binary
![Page 10: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/10.jpg)
CC = gccCFLAGS = -g -WallOUTPUT = binary
$(OUTPUT): lib.o prog.o$(CC) $(CFLAGS) lib.o prog.o -o binary
lib.o: lib.c$(CC) $(CFLAGS) -c lib.c -o lib.o
prog.o: prog.c$(CC) $(CFLAGS) -c prog.c -o prog.o
clean:rm *.o $(OUTPUT)
![Page 11: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/11.jpg)
CC = gccCFLAGS = -g -WallOUTPUT = binary
$(OUTPUT): lib.o prog.o$(CC) $(CFLAGS) lib.o prog.o -o binary
lib.o: lib.c$(CC) $(CFLAGS) -c lib.c -o lib.o
prog.o: prog.c$(CC) $(CFLAGS) -c prog.c -o prog.o
clean:rm *.o $(OUTPUT)
![Page 12: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/12.jpg)
CC = gccCFLAGS = -g -WallOUTPUT = binaryOBJFILES = lib.o prog.o
$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) -o binary
lib.o: lib.c$(CC) $(CFLAGS) -c lib.c -o lib.o
prog.o: prog.c$(CC) $(CFLAGS) -c prog.c -o prog.o
clean:rm *.o $(OUTPUT)
![Page 13: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/13.jpg)
CC = gccCFLAGS = -g -WallOUTPUT = binaryOBJFILES = lib.o prog.o
$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) -o binary
lib.o: lib.c$(CC) $(CFLAGS) -c lib.c -o lib.o
prog.o: prog.c$(CC) $(CFLAGS) -c prog.c -o prog.o
clean:rm *.o $(OUTPUT)
![Page 14: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/14.jpg)
CC = gccCFLAGS = -g -WallOUTPUT = binaryOBJFILES = lib.o prog.o
$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) -o binary
%.o: %.c# $<: dependency (%.c)# $@: target (%.o)$(CC) $(CFLAGS) -c $< -o $@
clean:rm *.o $(OUTPUT)
![Page 15: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/15.jpg)
Simple Test Script
% ./server 6667 &% cat testfile.01 | ./testscript.py% cat testfile.02 | ./testscript.py% killall -9 server
![Page 16: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/16.jpg)
Simple Test Script#/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 17: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/17.jpg)
CC = gccCFLAGS = -g -WallOUTPUT = binaryOBJFILES = lib.o prog.o
all: $(OUTPUT)
$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) -o binary
%.o: %.c# $<: dependencies (%.c)# $@: target (%.o)$(CC) $(CFLAGS) -c $< -o $@
clean:rm *.o $(OUTPUT)
![Page 18: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/18.jpg)
CC = gccCFLAGS = -g -WallOUTPUT = binaryOBJFILES = lib.o prog.o
all: $(OUTPUT) test
$(OUTPUT): $(OBJFILES)$(CC) $(CFLAGS) $(OBJFILES) -o binary
%.o: %.c# $<: dependencies (%.c)# $@: target (%.o)$(CC) $(CFLAGS) -c $< -o $@
test: $(OUTPUT)sh ./testscript.sh
clean:rm *.o $(OUTPUT)
![Page 19: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/19.jpg)
Use Makefile
% make% make test% make clean
Google– “makefile example”– “makefile template”– “make tutorial”
![Page 20: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/20.jpg)
Useful Unix Commands
• find “func_name” in files% grep -r func_name .
• replace “bad_func_name” to “good_func_name”% sed -e “s/bad_func_name/good_func_name/g”\
prog.c > prog.c.new
![Page 21: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/21.jpg)
Useful Unix Commands
• find a file named “prog.c”% find -name prog.c
• download files from Internet% wget http://address/to/file.tar.gz
• untar and unzip the file% tar xzvf file.tar.gz
![Page 22: Makefiles & Project 1 Q&A](https://reader035.vdocuments.us/reader035/viewer/2022081507/56815a82550346895dc7ed81/html5/thumbnails/22.jpg)
Project 1
• Checkpoint 2– Echo server– Handle multiple clients– Handle TCP framing
• Q & A