Small project Makefile

12, Aug, 2015

Too much coding time is setting stuff up rather than making things. I use IDEOne a lot for snippets of code. Sometimes I even brave Javascript for the convenience of having a file that can run on any computer with a web browser.

But there are times when you have to bite the bullet and split something into several files. This usually means setting up a project file in an IDE, after installing 8 gig of updates. Bah!

Here’s a Makefile I wrote to get projects up and running quickly. It takes a folder of code and makes an executable. Nothing fancy, but it does the job. Works on Linux and Mac OS X (Get the Command Line Tools if you don’t want XCode).

APPLICATION= App.exe
CPP= g++
CPPFLAGS=-Wall -Werror -pedantic
BUILD_DIR= ../Build
SOURCE_DIR= ../Source

SOURCES=$(shell find $(SOURCE_DIR) -name *.cpp | tr '' '/')
CLASSES=$(SOURCES:$(SOURCE_DIR)%.cpp=$(BUILD_DIR)%.o)

all: $(APPLICATION)

$(APPLICATION): $(CLASSES)
	$(CPP) $(CLASSES) -o $(BUILD_DIR)/$(APPLICATION)

$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.cpp
	$(CPP) $(CPPFLAGS)  -c $< -o $@

clean:
	@rm -r $(BUILD_DIR)/*