使用 makefile 编译 petsc 包含的代码,“.f90”失败但通过“.F90”

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

我正在 Linux 虚拟机上学习 petsc (3.13.1)。 编译器是用gfortran编译的mpich-3.2.1。

在浏览教程时,我发现 extensinos 是 .F90, 在我将分机号更改为 .f90.

后文件不起作用

我在 makefile 中为 .f90 添加了一些规则,仍然没有用。

我想知道如何修改 .f90 扩展的 makefile。

测试代码:

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

另存为 test_f90.f90test_F90.F90

test_f90.f90 无效

[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

而 test_F90.F90 通过了

[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

makefile 修改自$PETSC_DIR/share/petsc/Makerfile.user

其中添加了以下规则

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

完整的 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) $<

感谢 Mark 和 evets。 我通过将 -cpp 标志添加到 FFLAGS 来修复它 检查预处理部分后 手册 https://linux.die.net/man/1/gfortran

makefile修改部分:

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