Cpp makefiles

From wikinotes


Makefiles are automation built around sh-scripts, which allow you to only require recompiling of changed elements when recompiling C++ projects.

Gnu Makefile conventions https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html
Text Function Reference https://www.gnu.org/software/make/manual/html_node/Text-Functions.html


Background/Usage

make					## compile
make -d v    		## additional debug information
make clean			## delete all .o files and program

See make for more details.


Example

See make for more detailed explanation.


PROG   = helloworld
OBJS   = helloworld.cpp my_library.o
CC     = g++                              ## name of compiler
DEBUG  = -g                               ## 'debug' mode flag (so you can debug with gdb)
CFLAGS = -Wall -c $(DEBUG)                ## flags used for compiling/creating object files (Wall prints warnings)
LFLAGS = -Wall $(DEBUG)                   ## 'flags' used to determine libraries (Wall prints warnings) 


## program
$(PROG): $(OBJS)
	$(CC) $(LFLAGS) $(OBJS) -o $(PROG)

my_library.o:  my_library.cpp   my_library.h   my_other_library.h
	$(CC) $(CFLAGS) my_library.cpp

my_other_library.o:  my_other_library.cpp my_other_library.h
	$(CC) $(CFLAGS) my_library.cpp

Sources

http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/makefile.html
http://mrbook.org/blog/tutorials/make/