A Generic Makefile (GNU Make)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
dejai
Chaos Rift Junior
Chaos Rift Junior
Posts: 207
Joined: Fri Apr 11, 2008 8:44 pm

A Generic Makefile (GNU Make)

Post by dejai »

I recently redesigned the makefile for the NTEngine (That post in the game development forum no one responds to!). So I decided to share it with you. I will go though and explain how it works. This should be useful for typical programs that are aiming to build on PC platforms. Note if you are cleaver you make Makefile.platform makefiles then an overarching makefile that you can call make linux or make mac on.

Code: Select all

# NTEngine Makefile
# Dependencies: SFML-1.5, Boost-1_43 Headers (Bind, Function), liblua5.1.4

CPP=g++
INCLUDES=-I../include -I/usr/local/include  -I/usr/include/lua5.1

CFLAGS = ${INCLUDES} -g -Wall -O2 -pipe
LDFLAGS = -L/usr/local/lib
LDLIBS = -llua5.1 -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system

SRC := $(shell echo *.cpp)
OBJS := $(addprefix obj/, $(SRC:.cpp=.o))

all:    ${OBJS}
	$(CPP) -o ../../NTEngine ${OBJS} ${LDFLAGS} ${LDLIBS}

obj/%.o: %.cpp
	${CPP} -c -o $@ ${CFLAGS} $<

clean:
	rm obj/*.o
I have included the deps for my project as an example, but this is a neat way of getting around the issue of having to write all your source files in the SRC variable as it simply scans the current directory and finds them all for you. With slight modifications you could also add recursively. This is thanks to the $(shell command) that is provided in gnu mage. It makes my life a lot easier. Also it creates an obj/ folder so you do not have those .o files hanging around (actually it requires an obj folder to exist) I am not a guru at makefiles at all but I find this one serves me well. Please leave any questions or feedback below. This is designed for people who know a bit about gcc and who have dappled in make files if you are a gun at makefiles no need to be a dick.
Post Reply