C++ Training: Makefiles

The following is a basic makefile that worked for me.  However, there’s a link at the bottom to a forum post that provides a better solution.

#The c++ compiler you'd like to use:
CC=g++

#the following is for normal use:
CFLAGS=-c -O3 -Wall
LDFLAGS=
#the following is for using gprof or valgrind (only comment out one at a time)
#CFLAGS=-g -pg -c -O3 -Wall
#LDFLAGS=-pg

#All your .cpp files go below. This simple example assumes they're all in the same directory:
SOURCES=calculations.cpp Dates.cpp Demands.cpp triangleSimulation.cpp

OBJECTS=$(SOURCES:.cpp=.o)

#Below you place your target name, what you want the program to be called.
EXECUTABLE=triangleSimulation

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC)  $(CFLAGS) $^ -o $@

clean:
    rm -rf *.o $(EXECUTABLE)

Forum post with advice on a simple makefile

Advertisements

4 thoughts on “C++ Training: Makefiles

  1. Pingback: Compiling OpenSees on the Janus cluster | Water Programming: A Collaborative Research Blog

  2. Pingback: How to set up MinGW for the first time | Water Programming: A Collaborative Research Blog

  3. Pingback: Compiling, running, and linking a simulation model to Borg: LRGV Example | Water Programming: A Collaborative Research Blog

  4. Pingback: Water Programming Blog Guide (Part I) – Water Programming: A Collaborative Research Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s