makefiles

By Hal Canary, 2012-02-23 12:00:00 (link)

# Example (GNU) Makefile for a C++ project.
# Assume there is a .h file for each .cxx

executable_file_name = program
cxxfiles = \
  file_one.cxx \
  file_two.cxx \
  file_three.cxx \
  file_four.cxx

CXX = g++
CXXFLAGS = -g -Wall -std=c++0x
objects := $(cxxfiles:.cxx=.o)
headers := $(cxxfiles:.cxx=.h)

##  If you use other extensions:
# objects := $(cxxfiles:.cpp=.o)
# objects := $(cxxfiles:.cc=.o)
# headers := $(cxxfiles:.cxx=.hh)

.PHONY : clean all
all : $(executable_file_name)
$(executable_file_name) : $(objects)
	$(CXX) $(CXXFLAGS) $^ -o $@
%.o: %.cxx $(headers)
	$(CXX) -c $(CXXFLAGS) $< -o $@
clean:
	rm -f -v $(objects)

# Example (GNU) Makefile for a Java project.
# Constructs a JAR file with all of the classes.

entrypointclass = Program
jarfile = program.jar
javafiles = \
  file_one.java \
  file_two.java \
  file_three.java \
  file_four.java

JAVAC = javac
JAVA = java
JAR = jar
RM = rm -f
JCFLAGS = -Xlint -g
JFLAGS = -enableassertions
classes := $(javafiles:.java=.class)

.PHONY : clean all run
all: $(jarfile)
run: $(jarfile)
	$(JAVA) $(JFLAGS) -jar $(jarfile)
clean:
	$(RM) -v $(classes)
$(jarfile): $(classes)
	$(JAR) cvfe $@ $(entrypointclass) $^
%.class : %.java
	$(JAVAC) $(JCFLAGS) $^

(back)