This is a follow up on questions 231229, 8855896 and 35234003: I have a number of utility modules in ~/Fortran/include and I am developing new code in ~/Fortran/ProjectA. My dream Makefile would see that A.f90 needs mod_MyMod, and check in ~/Fortran/include to see if mod_mymod.mod and mod_MyMod.o exist, if not it would generate and place them in include, and then, after linking objects from both include and ProjectA it would produce A in ProjectA
So far I have this:
FC = gfortran
PROGRAMS = A
FCFLAGS = -I../include
LDFLAGS = -lm -L/usr/local/lib
# flags forall (e.g. look for system .mod files, required in gfortran)
# "make" builds all
all: $(PROGRAMS)
# Using Fortran MODULES:
A.o: ../include/mod_MyMod.o
A: ../include/mod_MyMod.o
# Linking
%: %.o
$(FC) -o $@ $^ $(LDFLAGS)
# Compiling (fortran)
%.o: %.f90
$(FC) $(FCFLAGS) -c $<
# Utility targets
clean:
rm -f *.o *.mod
in ProjectA/ I have
Program A
use, intrinsic :: iso_fortran_env, only: R64 => real64,I32 => int32,&
& input_unit,output_unit,error_unit
use mod_MyMod
implicit none
! global variables
character(len=23) :: string="This is A speaking"
call Print_This(string)
End Program A
in include/ I have
Module mod_MyMod
use, intrinsic :: iso_fortran_env, only: R64 => real64,I32 => int32,&
& input_unit,output_unit,error_unit
implicit none
contains
Subroutine Print_This(string)
character(len=20),intent(in) :: string
write(output_unit,"(*(g0,:,' '))")string
End Subroutine Print_This
End Module mod_MyMod
In order to accomplish what I want, I think I need separate rules for compiling A.f90 and mod_MyMod.f90. In the first case the object is pointed to the local directory, in the second the .mod and .o are to be put in include.