Search code examples
modulefortranintel-fortran

Loading only one subroutine from module


I have a module in which I coded a bunch of subroutines I frequently use. However, not every time I need all of them. Is is possible to load only one (or two...) subroutine from that module?

My interest in doing this is that there is one particular subroutine that requires another module to work, so if I'm not going to use that subroutine I would prefer to avoid loading that extra module.

Here is an example of what I have

MODULE MYLIBRARY
IMPLICIT NONE
CONTAINS

  SUBROUTINE PROC1
    PRINT *, 'This is PROC1'
  END SUBROUTINE PROC1

  SUBROUTINE PROC2
    USE EXTRAMOD
    PRINT *, 'This is PROC2'
  END SUBROUTINE PROC2

END MODULE MYLIBRARY

I would like to be able to load in my main program PROC1 only, so I wouldn't have to load the module EXTRAMOD (actually I don't even want to need that file). In my main program, I tried with the option ONLY but that only seems to work with variables and not subroutines. Here is an example of the main file

PROGRAM MAIN    
  USE MYLIBRARY, ONLY : PROC1
  IMPLICIT NONE
  CALL PROC1  
END PROGRAM Main

When I compile this, I get the error "Error in opening the compiled module file. Check INCLUDE paths. [EXTRAMOD]".

Any thoughts?


Solution

  • What you want to do is not possible with that compiler, unless you change the source of the MYLIBRARY module so that it no longer references EXTRAMOD.

    (The ONLY specifier can indeed limit the identifiers from a module that are accessible in a given scope, including identifiers for procedures, but that is not relevant to your underlying problem.)

    Note that behaviour you are seeing, for the given source, is compiler specific.

    With that compiler the information stored in a mod file (which is a compiler specific file used to communicate information from a module to the place where a module is used) that results from compiling a module is generally limited to the things immediately declared in that module. This means that in the case where a module references another module, the compiler then generally needs to find the mod file for that other module when the original module is used.

    Other compilers may make each mod file complete - storing both the things immediately declared in the module and anything referenced from other modules. This reduces the number of files that need to be distributed, but results in larger mod files.

    If the two subroutines are completely independent of each other, then you can split the source of the MYLIBRARY module into two, and only use the part of the split that contains the module that you need.

    If the entities in EXTRAMOD are not part of the characteristics of PROC2 (e.g. if you use a type defined in EXTRAMOD as the type of an arguments of PROC2, then entities in EXTRAMOD are being used as part of the characteristics), then you may be able to move the definition of PROC2 into a submodule. This would also break the dependence of the mod file for MYLIBRARY on the mod file for EXTRAMOD.