This is Lite Plone Theme
You are here: Home Developer Guide Makefile Templates

Makefile Templates

Some Makefile templates for C and Fortran programs that you can copy and customize as you wish.

A Makefile is a collection of instructions to automate the compilation of programs. Having one for your program is a great time saver if you are developing code yourself and often changing source files, and specially if you are planning to share the code with other people.

Creating a Makefile is very easy, you just need to specify your source files and the rules that build your program. Once you have a working Makefile, you can build your program by invoking 'make' or 'make all'.

There are many tutorials online about creating and managing Makefiles, see for instance the Introduction to Makefiles from the GNU make manual.

We have compiled a few templates that you can edit and use to build your programs on Cosmos.

C Makefile Template

# Makefile template for C programs 
# Needs filling in LIBS, CFILES and bin1/bin2 
# (replace all instances of bin1/bin2 with your program name(s))

# compilers, flags and libraries 
CC  := icc 
CFLAGS := -g -O3 -xHost -align -ansi-alias -mcmodel=medium

# BLAS, LAPACK, FFT (not FFTW) routines are widely used, so 
# can include them here by default
LIBS := -mkl=sequential

# optional - directories for sources and objects 
SRCDIR := .
OBJDIR := .

# source file(s) without suffix 
CFILES = 
OBJFILES = $(CFILES:%=%.o)
HEADERS = 

# name of your program(s) 
PROGRAMS = bin1 bin2

# program driver source file
bin1 =     
bin2 =

# rules
all : $(PROGRAMS)

bin1: $(CFILES:%=$(OBJDIR)/%.o) $(bin1:%=$(OBJDIR)/%.o)
	$(CC) $(CFLAGS) -I$(SRCDIR) $^ -o $@ $(LIBS)

bin2: $(CFILES:%=$(OBJDIR)/%.o) $(bin2:%=$(OBJDIR)/%.o)
        $(CC) $(CFLAGS) -I$(SRCDIR) $^ -o $@ $(LIBS)

# implicit rule - objects from source files
$(OBJDIR)/%.o: $(SRCDIR)/%.c $(HEADERS:%=$(SRCDIR)/%.h)
	$(CC) $(CFLAGS) -I$(SRCDIR) -c $< -o $@ 

clean:
	rm -f $(OBJDIR)/*.o $(PROGRAMS)

Fortran Makefile Template

# Makefile template for Fortran90 programs 
# Needs filling in LIBS, FFILES and bin1/bin2 
# (replace all instances of bin1/bin2 with your program name(s))

# compilers, flags and libraries 
F90  := ifort
FFLAGS := -g -O3 -xHost -align -ansi-alias -mcmodel=medium -traceback

# BLAS, LAPACK, FFT (not FFTW) routines are widely used, so 
# can include them here by default
LIBS := -mkl=sequential

# optional - directories for sources and objects 
SRCDIR := .
OBJDIR := .

# source file(s) without suffix 
FFILES = 
OBJFILES = $(FFILES:%=%.o)

# name of your program(s) 
PROGRAMS = bin1 bin2

# program driver source file
bin1 =     
bin2 =

# rules
all : $(PROGRAMS)

bin1: $(FFILES:%=$(OBJDIR)/%.o) $(bin1:%=$(OBJDIR)/%.o)
	$(F90) $(FFLAGS) -I$(SRCDIR) $^ -o $@ $(LIBS)

bin2: $(FFILES:%=$(OBJDIR)/%.o) $(bin2:%=$(OBJDIR)/%.o)
        $(F90) $(FFLAGS) -I$(SRCDIR) $^ -o $@ $(LIBS)

# implicit rule - objects from source files
$(OBJDIR)/%.o: $(SRCDIR)/%.f90
	$(F90) $(FFLAGS) -I$(SRCDIR) -c $< -o $@ 

clean:
	rm -f $(OBJDIR)/*.o $(PROGRAMS)