Make build-automation System (Makefiles)

GNU make
This file documents the GNU make utility, which determines automatically which pieces of a large program need to be recompiled, and issues the commands to recompile them. This is Edition 0.75, last updated 17 January 2020, of The GNU Make Manual, for GNU make version 4.3.
https://www.gnu.org/software/make/manual/make.html

Basic Structure

VARNAME = stuff 

TARGET : PREREQUISITES
    RECIPE $(VARNAME)

Terminal stuff

# creates a makefile
$ nano makefile

# runs first line of make file
$ make

# runs specified command
$ make main.o

Example

CXXFLAGS = -std=c++17 -Wall -I '../headers'

power-tests : power.o main.o
	g++ power.o main.o $(CXXFLAGS) -o 'power-tests'

main.o : main.cpp ../headers/power.h
	g++ -c main.cpp $(CXXFLAGS)

power.o : power.cpp ../headers/power.h
	g++ -c power.cpp $(CXXFLAGS)

clean :
	rm power-tests main.o power.o

VERY GOOD EXAMPLE

INCLUDEDIR = ../headers/
CXXFLAGS   = -std=c++17 -I $(INCLUDEDIR) -Wall -Wfatal-errors

# makes it so that anything ending with .h, is dir is set to the VAR
vpath %.h $(INCLUDEDIR)

# makes all and cleans all
all: random_prime-batch_process check_prime-console_app

clean:
	rm -f *.o random_prime-batch_process check_prime-console_app

.PHONY: all clean


## Executables
# Building an executable involves linking all object files used in that program.

random_prime-batch_process: random_prime-batch_process.o isPrime.o
	g++ $(CXXFLAGS) random_prime-batch_process.o isPrime.o -o random_prime-batch_process

check_prime-console_app: check_prime-console_app.o isPrime.o
	g++ $(CXXFLAGS) check_prime-console_app.o isPrime.o -o check_prime-console_app


## Object Files
# Building an object file depends on its .cpp file, and any included headers.
# Note the use of '-c' to specify compile-only (no linking).

random_prime-batch_process.o: random_prime-batch_process.cpp isPrime.h
	g++ $(CXXFLAGS) -c random_prime-batch_process.cpp

check_prime-console_app.o: check_prime-console_app.cpp isPrime.h
	g++ $(CXXFLAGS) -c check_prime-console_app.cpp

isPrime.o: isPrime.cpp isPrime.h
	g++ $(CXXFLAGS) -c isPrime.cpp