Linux - Makefile
As is well-known, makefile is a text file which specifies the behavior of make. According to the rules in makefile, a project can be automatically built in a fairly smart way.
Given a project, different from Windows platform, where the required makefile is implicitly generated by the IDE in use; in Unix/Linux, it is of the most importance to compose a makefile. Only in this way, we can control the building and linking of the given project to maximum extent.
C++
A typical makefile for a C++ project can be shown below, where
- The compiler is GCC.
- The resultant binary is
prj. - 3rd-party library IT++ is used, which is installed at
/opt/itpp. - Shared library is used for debug, while static library is adopted for release.
- Openmp-based multithreading is utilized for release build.
- Doxygen-based documentation, with the configuration file
config.
CC := /usr/bin/g++
INC := $(foreach dirs, $(shell find $(shell pwd) -iname '*.h'), $(dirs))
SRC := $(foreach dirs, $(shell find $(shell pwd) -iname '*.cpp'), $(dirs))
OBJ := $(subst .cpp,.o, $(SRC))
BIN := prj
ifdef RELEASE
CFLAGS := `/opt/itpp/bin/itpp-config --cflags`
LFLAGS := `/opt/itpp/bin/itpp-config --static --libs`
CFLAGS += -fopenmp
LFLAGS += -lgomp
else
CFLAGS := `/opt/itpp/bin/itpp-config --debug --cflags`
LFLAGS := `/opt/itpp/bin/itpp-config --debug --libs`
endif
LFLAGS += -lm
%.o: %.cpp
@echo "Building object file $@ ..."
@$(CC) -c -o $@ $< $(CFLAGS)
$(BIN): $(OBJ)
@echo "Linking object files and creating binary program ..."
@$(CC) $^ -o $@ $(LFLAGS)
.phony: default clean clear doc
default: $(BIN)
clear:
@echo "Removing all object files and the binary file ..."
@-rm -f $(OBJ) $(BIN)
clean:
@echo "Removing all object files ..."
@-rm -f $(OBJ)
doc:
@echo "Documenting ..."
@doxygen -s config .
LaTeX
A counterpart for \(\LaTeX\) can be shown below.
# Generic make file for LaTeX: requires GNU make
#
# This makefile provides four targets: dvi, ps, pdf and clean.
# The default is "pdf".
# To make a dvi file, type "make dvi"
# To make a ps file, type "make ps".
# To make a pdf file, type "make pdf" or simply "make".
# To remove all files generated by make, type "make clean".
TEXFILE = main.tex
.PHONY: dvi ps pdf clean
pdf: $(TEXFILE:.tex=.pdf)
ps: $(TEXFILE:.tex=.ps)
dvi: $(TEXFILE:.tex=.dvi)
%.dvi: %.tex
latex $<; \
bibtex $(<:.tex=.aux);\
latex $<;
%.ps: %.dvi
dvips -q -t a4 $< -o $(<:.dvi=.ps)
%.pdf: %.ps
ps2pdf -dPDFSETTINGS=/prepress $<
clean:
@rm -f \
$(TEXFILE:.tex=.aux) \
$(TEXFILE:.tex=.log) \
$(TEXFILE:.tex=.out) \
$(TEXFILE:.tex=.dvi) \
$(TEXFILE:.tex=.pdf) \
$(TEXFILE:.tex=.ps)