Makefile
Seen in CS247 - Software Engineering Principles. CS247 Lecture 8 and CS247 Lecture 7
Once you master how Makefile works, move on to CMake.
Resources:
- https://makefiletutorial.com/ (REALLY GOOD)
Example:
CXX=g++
CXXFLAGS= -std=c++14 -Wall -g -MMD
EXEC = myprogram
OBJECTS=${CCFILES:.cc=.o}
DEPEND=${CCFILES: .cc=.d}
${EXEC}: ${OBJECTS}
${CXX} ${OBJECTS} -o ${EXEC}
-include ${DEPENDS}Example:
main.cc(includesList.h)List.hList.cc(includesList.h)
:=vs.=SyntaxTurns out that you can also use
:=to define the variables. The difference is that if you assign it to another variable using:=, you need to make sure that the variable is defined before.
The ESENCE OF MAKE: target: preqrequisite
- this is how make is able to detect changes (they use timestamps of files as a heuristic)
Makefile (v1)
myprogram: main.o, List.o
myprogram (TAB) g++ main.o List.o -o myprogram
- General format for first line is
target: dependencies g++ main.o List.o -o myprogramis called the recipe
main.o : main.cc List.h
g++ -std=c++14 main.cc -c
List.o : List.cc List.h
g++ -std=c++14 main.cc -c
This text is in a Makefile in our directory.
make → creates the first target
Looks at the last modified time of dependencies.
If last modified time is newer for a dependency than a target ⇒ target is out of date, recreate it.
Still too much work! Still requires lots of updates to our makefile.
Makefile(V2)
CXX = g++
CXXFLAGS = -std=c++14 -Wall -g -MMD
EXEC = myprogram
CCFILES = $(wildcard *.cc)
OBJECTS = ${CCFILES:.cc=.o}
DEPENDS = ${CCFILES:.cc=.d}
${EXEC}: ${OBJECTS}
${CXX} ${CXXFLAGS} ${OBJECTS} -o ${EXEC}
-include ${DEPENDS}
For CXXFLAGS
-Wallfor more warnings-gfor extra debugging support (so you can use gdb)-MMDto generate dependency information as a side-effect of compilation. The compiler will create a.dfile for each.cppfile you compile. These.dfiles are makefile fragments that the make program can use to determine which.cppfiles need to be recompiled when a header file changes.
- include ${DEPENDS}
- Compiles all object files with dependencies using
CXXandCXXFLAGS
CXXFLAGS doesn't seem to be used??
How does it know to use those flags then? StackOverflow has same question.