# Copyright (C) 2012 W. Trevor King # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # Declare variables describing package information. # http://www.gnu.org/software/make/manual/html_node/Setting.html # The $(...) syntax references a variable's value. # http://www.gnu.org/software/make/manual/html_node/Reference.html # You can override variables from the command line. For example # $ make VERSION=2 dist # will generate hw0-2.tar.gz COURSE = phys405 PACKAGE = hw0 VERSION = 1 RELEASE = $(COURSE)-$(PACKAGE)-$(VERSION) PROGRAM = hello_world # Define the source files that will be distributed in the tarball SOURCE = *.cpp COPYING Makefile README # Define a list of object files needed to link PROGRAM OBJECTS = $(PROGRAM).o # You may want to link agains external libraries. For example, to # link against the system math library, use. # LIBS = -lm LIBS = # Define useful programs (this makes it easy to swap in alternates) CP = cp GREP = grep MKDIR = mkdir RM = rm TAR = tar # Declare targets that do not generate files of the same name. # http://www.gnu.org/software/make/manual/html_node/Phony-Targets.html .PHONY: all help clean dist run # target: all - the default target all: $(PROGRAM) # target: help - display callable targets # Use `grep` to search this file for target comments help: $(GREP) '^# target:' [Mm]akefile # target: clean - remove automatically generated files clean: $(RM) -rf $(PROGRAM) *.o $(RELEASE)* # target: dist - generate a tarball packaging the source # Here, we move the source into a temporary release directory, tar the # release directory, and remove the release directory. dist: $(MKDIR) $(RELEASE) $(CP) -r $(SOURCE) $(RELEASE) $(TAR) -czf $(RELEASE).tar.gz $(RELEASE) $(RM) -rf $(RELEASE) # target: hello_world - compile the hello_world program # Use GCC to link the program from object files. $(PROGRAM): $(OBJECTS) $(CXX) $(LDFLAGS) -o $@ $^ $(LIBS) # target: run - use the program for its intended purpose # Here we just execute PROGRAM, but you could also use something like # run: $(PROGRAM) plot.gp # ./$(PROGRAM) > data # gnuplot plot.gp # where plot.gp was a gnuplot script for plotting data generated by # PROGRAM. run: $(PROGRAM) ./$(PROGRAM) # Matching rule for compiling object files from C++ source # There is an implicit rule for this in GNU make # http://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html # but I redefine it here for clarity. # # CXX and CXXFLAGS have defaults defined by make # http://www.gnu.org/software/make/manual/html_node/Implicit-Variables.html # but they can also be come from the environment # http://www.gnu.org/software/make/manual/html_node/Environment.html # for example, try # $ make CXX=/usr/local/bin/g++ CXXFLAGS=-Wall %.o: %.cpp $(CXX) $(CXXFLAGS) -c $<