command]
```
- For **`target`** to be generated, the **`prerequisites`** must all exist (or be generated if necessary)
- **`target`** is generated by executing the specified **`command`**
- **`target`** is generated only if it does not exist, or if one of the prerequisites is more recent
- Prevents from building everything each time, but only what is necessary
## Commenting
Lines prefixed with `#` are not evaluated
```makefile
# This is a comment
```
---
layout: true
# Version 0.1
---
## Basic rules
```makefile
myfact: main.o fact.o
g++ -Wall -Werror -std=c++11 -o myfact main.o fact.o
main.o: main.cc fact.h
g++ -Wall -Werror -std=c++11 -c -o main.o main.cc
fact.o: fact.cc fact.h
g++ -Wall -Werror -std=c++11 -c -o fact.o fact.cc
```
```bash
$ make
g++ -Wall -Werror -std=c++11 -c -o main.o main.cc
g++ -Wall -Werror -std=c++11 -c -o fact.o fact.cc
g++ -Wall -Werror -std=c++11 -o myfact main.o fact.o
```
---
## `all` rule
```makefile
all: myfact
myfact: main.o fact.o
g++ -Wall -Werror -std=c++11 -o myfact main.o fact.o
main.o: main.cc fact.h
g++ -Wall -Werror -std=c++11 -c -o main.o main.cc
fact.o: fact.cc fact.h
g++ -Wall -Werror -std=c++11 -c -o fact.o fact.cc
```
```bash
$ make
g++ -Wall -Werror -std=c++11 -c -o main.o main.cc
g++ -Wall -Werror -std=c++11 -c -o fact.o fact.cc
g++ -Wall -Werror -std=c++11 -o myfact main.o fact.o
```
---
## `clean` rule
```makefile
all: myfact
...
clean:
rm -f myfact main.o fact.o
```
```bash
$ make
g++ -Wall -Werror -std=c++11 -c -o main.o main.cc
g++ -Wall -Werror -std=c++11 -c -o fact.o fact.cc
g++ -Wall -Werror -std=c++11 -o myfact main.o fact.o
$ make clean
rm -f myfact main.o fact.o
```
---
## A first and basic Makefile
```makefile
all: myfact
myfact: main.o fact.o
g++ -Wall -Werror -std=c++11 -o myfact main.o fact.o
main.o: main.cc fact.h
g++ -Wall -Werror -std=c++11 -c -o main.o main.cc
fact.o: fact.cc fact.h
g++ -Wall -Werror -std=c++11 -c -o fact.o fact.cc
clean:
rm -f myfact main.o fact.o
```
---
layout: true
# Version 0.1
---
## How to avoid redundancy...?
*A good programmer is a lazy programmer!*
all: myfact
myfact: main.o fact.o
g++ -Wall -Werror -std=c++11 -o myfact main.o fact.o
main.o: main.cc fact.h
g++ -Wall -Werror -std=c++11 -c -o main.o main.cc
fact.o: fact.cc fact.h
g++ -Wall -Werror -std=c++11 -c -o fact.o fact.cc
clean:
rm -f myfact main.o fact.o
---
layout: true
# Version 1.0
---
## Using Variables
- Use variables to avoid redundancy
- `CXX` for the C++ compiler
- `CXXFLAGS` for compiler flags
- Reference variables with `$(VAR_NAME)`
```makefile
CXX := g++
CXXFLAGS := -Wall -Werror -std=c++11
all: myfact
myfact: main.o fact.o
$(CXX) $(CXXFLAGS) -o myfact main.o fact.o
main.o: main.cc fact.h
$(CXX) $(CXXFLAGS) -c -o main.o main.cc
fact.o: fact.cc fact.h
$(CXX) $(CXXFLAGS) -c -o fact.o fact.cc
clean:
rm -f myfact main.o fact.o
```
```bash
$ make
g++ -Wall -Werror -std=c++11 -c -o main.o main.cc
g++ -Wall -Werror -std=c++11 -c -o fact.o fact.cc
g++ -Wall -Werror -std=c++11 -o myfact main.o fact.o
```
- Good enough for CS 10C!