Tuesday, January 19, 2016

Simple C++ Makefile on Linux

Seems a pain to get started with makefiles for C++ code on Ubuntu.

First create and name the file 'Makefile' in the same folder as your .h and .cpp files

Second copy in the following contents and change the .cpp filenames:

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=Test.cpp Foo.cpp Bar.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=Test

all: $(SOURCES) $(EXECUTABLE)
 
$(EXECUTABLE): $(OBJECTS)
Tab $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
Tab $(CC) $(CFLAGS) $< -o $@