A simple Makefile

Learning how to create a Makefile is one of those tasks every C/C++ programmer has to do. Since there are many good make tutorials on the web, I’m only going to share a simplistic makefile that you can use for your projects.

CFLAGS=-c -Wall -Iinclude
LDFLAGS=-lPocoFoundation -lPocoData
SOURCES=a.cpp b.cpp main.cpp
OBJECTS=$(SOURCES:.cpp=.o)
VPATH=src

all: pre $(OBJECTS)
	g++ $(OBJECTS) -o build/main $(LDFLAGS)

.cpp.o:
	g++ $(CFLAGS) $< -o $@

pre:
	mkdir -p build

clean:
	rm -Rf build
	rm -f *.o

 

This Makefile assumes the following directory structure: All .cpp files are located inside a directory named “src”. All .h files are located inside a directory named “include”. As an example I’m linking against the PocoFoundation and the PocoData libraries, you should change those to the libraries your project uses.

There are so many additional things you can do to grow this basic Makefile. My objective here was to create something really basic that you can use as start point for your projects. I hope you find it useful.

Leave a Reply