makefilefortrangnu-makegfortranpetsc

Compile petsc included code using a makefile, failed for '.f90' but passed for '.F90'


I'm leaning petsc (3.13.1) on a Linux virtual machine. The compiler is mpich-3.2.1 compiled with gfortran.

While going through the tutorials I found the extensinos are .F90, the files did not work after I changed the ext to .f90.

I added some rules for .f90 in the makefile, still, it did not work.

I'm wondering how to revise makefile for the .f90 extensions.

Test codes:

program test

#include <petsc/finclude/petscsys.h>
use petscsys

type(PetscInt) :: ierr
call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
if (ierr .ne. 0) then
    write(*,*) 'Unable to initialize PETSc'
    stop
end if
call PetscFinalize(ierr)

end program test

saved as test_f90.f90 and test_F90.F90

the test_f90.f90 did not work

[cfd@localhost tutorial]$ make test_f90
mpif90 -fPIC -Wall -ffree-line-length-0 -Wno-unused-dummy-argument -g -O -I/home/cfd/petsc-3.13.1/linux-gnu/include -I/home/cfd/petsc-3.13.1/include   -L/home/cfd/petsc-3.13.1/linux-gnu/lib    -Wl,-rpath,/home/cfd/petsc-3.13.1/linux-gnu/lib  -o test_f90 test_f90.f90 -lpetsc   -lm
Warning: test_f90.f90:3: Illegal preprocessor directive
test_f90.f90:6.14:

type(PetscInt) :: ierr
              1
Error: Derived type 'petscint' at (1) is being used before it is defined
make: *** [test_f90] Error 1

while the test_F90.F90 passed

[cfd@localhost tutorial]$ make test_F90
mpif90 -fPIC -Wall -ffree-line-length-0 -Wno-unused-dummy-argument -g -O -I/home/cfd/petsc-3.13.1/linux-gnu/include -I/home/cfd/petsc-3.13.1/include   -L/home/cfd/petsc-3.13.1/linux-gnu/lib    -Wl,-rpath,/home/cfd/petsc-3.13.1/linux-gnu/lib  -o test_F90 test_F90.F90 -lpetsc   -lm

The makefile is revised from $PETSC_DIR/share/petsc/Makerfile.user

where the following rules are added

% : %.f90
    $(LINK.F) -o $@ $^ $(LDLIBS)
%.o: %.f90
    $(COMPILE.F) $(OUTPUT_OPTION) $<

the full makefile:

# -*- mode: makefile -*-

PETSc.pc := $(PETSC_DIR)/$(PETSC_ARCH)/lib/pkgconfig/PETSc.pc
PACKAGES := $(PETSc.pc)

CC := $(shell pkg-config --variable=ccompiler $(PACKAGES))
CXX := $(shell pkg-config --variable=cxxcompiler $(PACKAGES))
FC := $(shell pkg-config --variable=fcompiler $(PACKAGES))
CFLAGS_OTHER := $(shell pkg-config --cflags-only-other $(PACKAGES))
CFLAGS := $(shell pkg-config --variable=cflags_extra $(PACKAGES)) $(CFLAGS_OTHER)
CXXFLAGS := $(shell pkg-config --variable=cxxflags_extra $(PACKAGES)) $(CFLAGS_OTHER)
FFLAGS := $(shell pkg-config --variable=fflags_extra $(PACKAGES))
CPPFLAGS := $(shell pkg-config --cflags-only-I $(PACKAGES))
LDFLAGS := $(shell pkg-config --libs-only-L --libs-only-other $(PACKAGES))
LDFLAGS += $(patsubst -L%, $(shell pkg-config --variable=ldflag_rpath $(PACKAGES))%, $(shell pkg-config --libs-only-L $(PACKAGES)))
LDLIBS := $(shell pkg-config --libs-only-l $(PACKAGES)) -lm

print:
    @echo CC=$(CC)
    @echo CXX=$(CXX)
    @echo FC=$(FC)
    @echo CFLAGS=$(CFLAGS)
    @echo CXXFLAGS=$(CXXFLAGS)
    @echo FFLAGS=$(FFLAGS)
    @echo CPPFLAGS=$(CPPFLAGS)
    @echo LDFLAGS=$(LDFLAGS)
    @echo LDLIBS=$(LDLIBS)

% : %.f90
    $(LINK.F) -o $@ $^ $(LDLIBS)
%.o: %.f90
    $(COMPILE.F) $(OUTPUT_OPTION) $<
% : %.F90
    $(LINK.F) -o $@ $^ $(LDLIBS)
%.o: %.F90
    $(COMPILE.F) $(OUTPUT_OPTION) $<
% : %.c
    $(LINK.c) -o $@ $^ $(LDLIBS)
%.o: %.c
    $(COMPILE.cc) $(OUTPUT_OPTION) $<
% : %.cxx
    $(LINK.cc) -o $@ $^ $(LDLIBS)
%.o: %.cxx
    $(COMPILE.cc) $(OUTPUT_OPTION) $<

Solution

  • Thanks for Mark and evets. I fix it by adding the -cpp flag to FFLAGS after checking the preprocessing part of the manual https://linux.die.net/man/1/gfortran

    The makefile revised part:

    FFLAGS += -cpp
    % : %.f90
        $(LINK.F) -o $@ $^ $(LDLIBS)
    %.o: %.f90
        $(COMPILE.F) $(FFLAGS) $(OUTPUT_OPTION) $<