Stand With Ukraine

Saturday, March 27, 2010

How to create a make file

Recently I had to deal with mixed c++ and fortran project, and decided to use eclipse for development, created managed make C/C++ project, and wrote Makefile. I used eclipse in order to speed up. I like the navigation and auto completion features, pretty lazy to type everything. I am aware of the perl script mkmf which is very good and I also used it, but for the simple project like this one I decided that it is easier to do it by hand.

How this works.
It is very simple you just have to specify input and output files and actions which should be performed to obtain the output. Schematically it looks like this:



resultfile(s): inputfile(s)
action


Note that the tabs are important.
Also you can specify ierarchical dependance, I like this feature very much:



...
result2 : result1
action2

result : result1
action1

all : result


Here is the example Makefile that I've created for that project.

##############Makefile


#set variables, compilers, libs, and others if you'll need them
CXX = pgCC
FC = pgf90

CXXFLAGS = -O2 -g -c
FCFLAGS = -i8 -r8 -byteswapio -c

OBJS = meshrouteX11.o readccc.o
CFILES = meshrouteX11.cpp
FORTRANFILES = readccc.f90

LIBS = -lg2c -pgf90libs $(CLIMLIB)/lib/losub.a -lm

TARGET = meshroute.exe

#create object files
$(OBJS): $(CFILES) $(FORTRANFILES)
$(CXX) $(CXXFLAGS) $(CFILES)
$(FC) $(FCFLAGS) $(FORTRANFILES)

#resultfile: inputfile
# action
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)

all: $(TARGET)

clean:
rm -f $(OBJS) $(TARGET)



Another simple example of the Makefile
##############Makefile


FC = gfortran

*.o : *.f90
$(FC) -c *.f90

all : *.o
$(FC) *.o -o demo.exe

clean:
rm -rf *.exe
rm -rf *.o


No comments: