Download - Introduction to Makefiles from Lab 04
![Page 1: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/1.jpg)
CSCI 3308 Fall 2014 Liz Boese
Makefiles
![Page 2: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/2.jpg)
MAKEFILES
![Page 3: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/3.jpg)
The g++ Compiler ◆ What happens when you call g++ to build your
program? ◆ Phase 1, Compilation:
.cpp files are compiled into .o object modules
◆ Phase 2, Linking: .o modules are linked together to create a single executable.
![Page 4: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/4.jpg)
The Makefile: Input for make
Simplify the compile process: ◆ Create a file called “Makefile”
– “make” command searches for “Makefile”
◆ Determines which pieces of a large program need to be compiled or recompiled, and issues commands to compile and link them in an automated fashion.
◆ Just type “make” at the terminal
![Page 5: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/5.jpg)
Makefiles in Professional C++
◆ Fast to run: Only recompile what you need to recompile upon code change, with a simple command
◆ Great for code versioning and deployment; just version the source code and the Makefile and quickly deploy on any compatible system (no need to version binaries).
![Page 6: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/6.jpg)
The Process
main.cpp blurb.cpp database.cpp
main.o blurb.o database.o
a.out libraries
Compiler
Linker
![Page 7: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/7.jpg)
How g++ Does All This ◆ Calling:
g++ <option flags> <file list> "
◆ When g++ is given .cpp files, it performs both compilation and linking.
◆ If given .o files, it performs linking only.
![Page 8: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/8.jpg)
A Basic Makefile
◆ A first Makefile
# build an executable named myprog"all: myprog.cpp"
"gcc -o myprog myprog.cpp""clean:"
"$(RM) myprog"
The tabs are important!
# starts a comment
![Page 9: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/9.jpg)
“clean” Target: Gotta Have One
◆ An important target that represents an action rather than a g++ operation.
◆ Has no dependencies, runs a command to remove all the compilation products from the directory, “cleaning” things up.
◆ Useful for porting code, getting rid of corrupted files, etc.
◆ Normally also removes the “core” file if it’s present from a past program meltdown.
◆ Call by typing “make clean” into prompt.
![Page 10: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/10.jpg)
A Basic Makefile
◆ A first Makefile: Create one for your Student program
# build an executable for Student example"all: Student.cpp Driver.cpp" g++ -std=c++11 -o studs Student.cpp \
"Driver.cpp""clean:" $(RM) studs"
A slash \ allows you to go to the
next line
![Page 11: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/11.jpg)
Sample Makefile ◆ Makefiles main element is called a rule: Example:
target : dependencies
TAB commands #shell commands
# -o to specify executable file name # -c to compile only (no linking)
my_prog : eval.o main.o g++ -o my_prog eval.o main.o
eval.o : eval.c eval.h g++ -c eval.c
main.o : main.c eval.h g++ -c main.c
![Page 12: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/12.jpg)
Makefile Lingo ◆ Target: Usually the name of an executable or
object file that is generated by g++, but it can also be the name of an action to carry out.
◆ Prerequisites: A list of files needed to create the target. If one of these files have changed, then the make utility knows that it has to build the target file again. Also called dependencies.
◆ Command: An action that make carries out, usually a g++ compilation or linking command. In make, commands are run and generate output just as if they were entered one by one into the UNIX prompt.
![Page 13: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/13.jpg)
Options For g++ Compilation ◆ -c
Compiles .cpp file arguments to .o but does not link (we’ll need this for “make” later).
◆ -o Specifies the name of the program to be linked together (instead of a.out)
◆ -std=c++11"◆ -Wall
Give warnings about code that’s probably in error. – For example, it will catch and report:
int number = GetRandomInteger() if (number = 6) cout << “Always equals 6!”;
![Page 14: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/14.jpg)
SEPARATING COMPILATION & LINKING
![Page 15: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/15.jpg)
The “textr” App Makefile, Take 1:
textr : main.o blurb.o database.o""g++ -o textr main.o blurb.o database.o "main.o : main.cpp ""g++ -Wall -c main.cpp"blurb.o : blurb.cpp blurb.h"
g++ -Wall -c blurb.cpp"database.o : database.cpp database.h""g++ -Wall -c database.cpp"clean:""$(RM) core textr main.o blurb.o database.o
![Page 16: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/16.jpg)
Makefile - Student
◆ Modify your Student Makefile to separate compilation and linking. Name your program ‘studs’ # build an executable for Student example"studs: Student.o Driver.o" g++ -o studs Student.o Driver.o"Student.o: Student.cpp Student.h" g++ -Wall -std=c++11 -c Student.cpp"Driver.o: Driver.cpp" g++ -Wall -std=c++11 -c Driver.cpp""clean:" $(RM) studs Student.o Driver.o"
![Page 17: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/17.jpg)
VARIABLES
![Page 18: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/18.jpg)
Next Step: Add Variables
OBJS = main.o blurb.o database.o"
textr : $(OBJS)" g++ -o textr $(OBJS)"main.o : main.cpp " g++ -Wall -c main.cpp"blurb.o : blurb.cpp blurb.h" g++ -Wall -c blurb.cpp"database.o : database.cpp database.h " g++ -Wall -c database.cpp"clean:" rm -f core textr $(OBJS)"
• Add a variable for the object files
![Page 19: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/19.jpg)
Makefile - Student ◆ Modify your Student Makefile to add a variable
for all the object files (much better, eh?) # build an executable for Student example"OBJS = Student.o Driver.o""
studs: $(OBJS)" g++ -o studs $(OBJS)"Student.o: Student.cpp Student.h" g++ -Wall -std=c++11 -c Student.cpp"Driver.o: Driver.cpp" g++ -Wall -std=c++11 -c Driver.cpp""
clean:" $(RM) studs $(OBJS)"
![Page 20: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/20.jpg)
Next Step: Add Variables
OBJS = main.o blurb.o database.o"CPPFLAGS = -Wall"textr : $(OBJS)" g++ -o textr $(OBJS)"main.o : main.cpp " g++ $(CPPFLAGS) -c main.cpp"blurb.o : blurb.cpp blurb.h" g++ $(CPPFLAGS) -c blurb.cpp"database.o : database.cpp database.h " g++ $(CPPFLAGS) -c database.cpp"clean:" rm -f core textr $(OBJS)"
• Add a variable for the compiler flags CPPFLAGS
![Page 21: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/21.jpg)
Makefile - Student ◆ Modify your Student Makefile to add a variable
for the compiler options # build an executable for Student example"OBJS = Student.o Driver.o"CPPFLAGS = -Wall –std=c++11""
studs: $(OBJS)" g++ -o studs $(OBJS)"Student.o: Student.cpp Student.h" g++ $(CPPFLAGS) -c Student.cpp"Driver.o: Driver.cpp" g++ $(CPPFLAGS) -c Driver.cpp""
clean:" $(RM) studs $(OBJS)"
![Page 22: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/22.jpg)
Variable for Compiler & Program #Makefile for “textr” C++ application"#Created by Dan Wilson 1/29/06 "
PROG = textr"CC = g++"CPPFLAGS = -g –Wall "OBJS = main.o blurb.o database.o""
$(PROG) : $(OBJS)" $(CC) -o $(PROG) $(OBJS)"main.o : main.cpp" $(CC) $(CPPFLAGS) -c main.cpp"blurb.o : blurb.cpp blurb.h" $(CC) $(CPPFLAGS) -c blurb.cpp"database.o : database.cpp database.h" $(CC) $(CPPFLAGS) -c database.cpp"clean:" " rm -f core $(PROG) $(OBJS)
![Page 23: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/23.jpg)
Makefile - Student ◆ Add variables for compiler & executable
# build an executable for Student example"PROG = studs"CC = g++"OBJS = Student.o Driver.o"CPPFLAGS = -Wall –std=c++11""
$(PROG): $(OBJS)" $(CC) -o $(PROG) $(OBJS)"Student.o: Student.cpp Student.h" $(CC) $(CPPFLAGS) -c Student.cpp"Driver.o: Driver.cpp" $(CC) $(CPPFLAGS) -c Driver.cpp""
clean:" $(RM) $(PROG) $(OBJS)"
![Page 24: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/24.jpg)
MAKE IS INTELLIGENT
![Page 25: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/25.jpg)
Next Step: Omit the Obvious!
PROG = textr"CC = g++"CPPFLAGS = -g –Wall "OBJS = main.o blurb.o database.o""
$(PROG) : $(OBJS)" $(CC) -o $(PROG) $(OBJS)"main.o : " $(CC) $(CPPFLAGS) -c main.cpp"blurb.o : blurb.h" $(CC) $(CPPFLAGS) -c blurb.cpp"database.o : database.h" $(CC) $(CPPFLAGS) -c database.cpp"clean:" " rm -f core $(PROG) $(OBJS)
• (make knows .cpp -> .o)
![Page 26: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/26.jpg)
Makefile - Student ◆ Modify your Student Makefile to remove the
unnecessary references to .cpp files (2 places only!) # build an executable for Student example"OBJS = Student.o Driver.o""
studs: $(OBJS)" g++ -o studs $(OBJS)"Student.o: Student.h" g++ $(CPPFLAGS) -c Student.cpp"Driver.o:" g++ $(CPPFLAGS) -c Driver.cpp""
clean:" $(RM) studs $(OBJS)"
![Page 27: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/27.jpg)
TROUBLESHOOTING
![Page 28: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/28.jpg)
Troubleshooting ◆ If you get this error message, it’s because your
tab is not correct "*** missing separator. Stop."
![Page 29: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/29.jpg)
ANT
![Page 30: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/30.jpg)
Ant ◆ Java version of a Makefile ◆ Relies on
– Targets – Dependencies – Commands
◆ Uses XML ◆ build.xml in project root directory ◆ From command-line, type:
ant ◆ Eclipse and other IDEs have it integrated
![Page 31: Introduction to Makefiles from Lab 04](https://reader034.vdocuments.us/reader034/viewer/2022051216/563dba59550346aa9aa4de4a/html5/thumbnails/31.jpg)
Example Ant file <?xml version="1.0"?> <project name="HelloJavaAnt" default="build"> <property name="src.dir" location="src" /> <property name="build.dir" location="build/classes" /> <target name="mkdir"> <mkdir dir="${build.dir}" /> <mkdir dir="${build.dir}/JARs" /> </target> <target name="build" depends="mkdir"> <javac srcdir="${src.dir}" destdir="${build.dir}"/> </target> <target name="compress" depends="build"> <jar destfile="${build.dir}/JARs" basedir="${build.dir}" </target> </project>