Friday 4 September 2009

A simple Makefile for multiple sub-directory projects

Creating Makefiles makes me almost want to cry. Here is a simple Makefile that took me ages to write. I hope that it can help get you started if you're having problems.

This took me bloody ages! I didn't know how to specify files that were in different directories. Online resources were way too complicated and yes, I did read the manual.

The part that almost killed me was the flag, -D_REENTRANT. I have no idea what it does but I need to compile because in one of my header files I had a number of #define's that were needed by several other files.

In Makefile (remember to put a real tab before 'g++'):

all :
g++ -g -Wall -D_REENTRANT \
test/TestGenome.cpp \
critter/brain.cp \
critter/BrainParam.cp \
utils/misc.cp \
critter/genome.cp \
-lGL -lboost_unit_test_framework -lboost_serialization \
-Iapp -Icritter -Ienvironment -Igraphics -Iui -Iutils \
-I/usr/local/Trolltech/Qt-4.4.3/include/QtCore \
-I/usr/local/Trolltech/Qt-4.4.3/include/QtGui \
-I/usr/local/Trolltech/Qt-4.4.3/include/QtOpenGL \
-I/usr/local/Trolltech/Qt-4.4.3/include \
-I/usr/include/GL

  • g++
    Use this if you are using C++ headers such as iostream.
  • -g
    Use this so that you can debug your code.
  • -Wall
    Use this to strictly detect errors at compile time.
  • -D_REENTRANT
    Use this so that things that what you #define can be used by several other files
  • test/TestGenome.cpp
    Use this to specify if your file is in subdirectory.
  • -lGL
    Use this to specify the library you want to link with. In my case I'm linking with OpenGL, hence the "GL" bit. Note that is a lower case l for Lima.
  • -Iapp
    Use this to specify directories to look in for header files. Note that is an upper case I for India.
  • \
    Use this to continue the command onto the next line. The program effectively treats it all as one line.

My directory structure:

Makefile
app/
critter/
environment/
graphics/
test/
ui/
utils/

No comments:

Post a Comment