James
James

Reputation: 105

Writing a Makefile to compile a .f program and codes written for gfortan compiler using a .f90 code written for Ifort

I have a set of .f codes written to be compiled in gfortran and a Makefile which makes a program.

I would like to use as additional code, NewCode.f90, in this program but this is a .f90 and are written to be compiled using Ifort. I have written a make file so that it compiles all the codes in ifort as this is needed for NewCode.f90:

FC=ifort

ARCH = linux

include SYS.$(ARCH)

MYFLGS = -O0 -pg -g -fbounds-check -Wall -fbacktrace -finit-real=nan
LINKFLAGS = -L$(MKLROOT)
FFLAGS = $(MYFLGS) -I$(INCDIR) -static

.SUFFIXES: .o .f .c .f90

.f.o:
    $(FC) $(FFLAGS) -c $*.f
NewCode.o NewCode.f90:
    $(FC) $(FFLAGS) -ffree-form NewCode.f90 -c
.c.o:
    $(CC) $(CFLAGS) -c $*.c

OBJ =   code1.o code2.o code3.o NewCode.o

default:    nonlinear

nonlinear:  setnl Program

setnl:
/bin/rm -f *.o *.bak core par.h
/bin/ln -sf INCLUDE/nonlinear.par par.h

clean:
/bin/rm -f *.o *.bak core par.h
/bin/rm -rf ../bin/*

cleanarch:
/bin/rm -f *.o *.bak core par.h
/bin/rm -f $(INSTDIR)/program


program:    program.f program.o $(OBJ) par.h
            $(FC) $(FFLAGS) $(LINKFLAGS) -o $@ program.o $(OBJ) $(LIBS)  -Wl,--start-group -L$(MKLROOT)/lib/intel64 -lmkl_gf_ilp64 -lmkl_core -lmkl_sequential -Wl,--end-group -lpthread

When I make this file it runs into problems compiling the files written in (I think) fortran 70 using the ifort compiler. For example the program calls the IPARITY function:

FUNCTION IPARITY(l)
IMPLICIT REAL*4 (A-H, O-Z)

k = l/2
kk = 2*k - l
IF ( kk.EQ.0 ) THEN
  IPARITY = 1
ELSE
  IPARITY = -1
END IF
RETURN
END

Calling it as, for example:

PRINT *,IPARITY(1)

When I compile this program using gfortran this function is compiled and called without any problems however when I compile it with ifort there are problems. It appears the compiler expects IPARITY to be an array:

An array-valued argument is required in this context. 

I have tried compiling the files written in fortran 70 using gfortran and the files in fortran 90 using ifort but I have not managed to get this quite right:

COMP=ifort
ARCH=linux 
...
...
.f.o:
    $(FC) $(FFLAGS) -c $*.f
NewCode.o NewCode.f90:
    $(COMP) $(FFLAGS) -ffree-form NewCode.f90 -c
.c.o:
    $(CC) $(CFLAGS) -c $*.c
...
...
...
program: program.f program.o $(OBJ) par.h
            $(FC) $(FFLAGS) $(LINKFLAGS) -o $@ program.o $(OBJ) $(LIBS)  -Wl,--start-group -L$(MKLROOT)/lib/intel64 -lmkl_gf_ilp64 -lmkl_core -lmkl_sequential -Wl,--end-group -lpthread

However weather I put the combiner as:

program:    program.f program.o $(OBJ) par.h
            $(FC) ...

Or

program:    program.f program.o $(OBJ) par.h
            $(COMP) ...

I get errors in the .f90 and .f codes respectively.

I think that what is needed is either a tag in the makefile:

  .f.o:
  $(FC) $(FFLAGS) -c $*.f

Which will tell the compiler that it is reading fortran 70 code but still be compiled correctly. Or a flag in the combiner line which will allow the different codes compiled using different compilers to make the program together.

Any advice would be very welcome.

Thank you very much

James

Upvotes: 1

Views: 1193

Answers (1)

Your code is still very incomplete, but I will at least try to guess from your snippet what is in the top secret rest.

Probably, you fail to declare IPARITY as EXTERNAL although you always should do that for external functions. There is different function called IPARITY in Fortran 2008 so the compiler thinks you want this one. To tell it you want your own external procedure declare it as EXTERNAL.

     EXTERNAL IPARITY
     integer iparity
     ...
     print *,IPARITY(4)

or use an interface block if you use Fortran 90 or newer

     interface
       integer function iparity(i)
         integer i
       end function
     end interface
     ...
     print *,IPARITY(4)

For all your future programs i strongly recommend to use at least Fortran 90 and use implicit none in all scopes. Also, by using modules you avoid these problems and get many other advantages.

Upvotes: 3

Related Questions