Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28894 Discussions

Access subroutine inside a module using a linked library

Lorenzo_W_
Beginner
662 Views

I would like to use a subroutine written inside a static library (written in Fortran) inside a different Fortran executables.
This is my working example:

    subroutine mysum(a,b,c)
        implicit none
        real*8, intent(in)  :: a,b
        real*8, intent(out) :: c
        !
        c = a + b
        !
    end subroutine

That generates a XXX.lib file that I am pointing at with this main:

program main
    implicit none
    !
    real*8 :: var1, var2
    real*8 :: out1
    !
    var1 = 15.0
    var2 = 10.0
    call somma(var1,var2,out1)
    !
    write(*,*) out1
    !
end program

Everything is working, but the problem arise when I want to have my subroutine defined inside a module, like this:

module mymodule
contains
    !
    subroutine mysum(a,b,c)
        implicit none
        real*8, intent(in)  :: a,b
        real*8, intent(out) :: c
        !
        c = a + b
        !
    end subroutine
end module

At this point the "main" compiler fails in finding the subroutine. Is there a way to let the compiler "see" the subroutine declared inside the module?

 

Thank you

0 Kudos
4 Replies
Steve_Lionel
Honored Contributor III
662 Views

You must USE the module to make its contents visible. Add USE MYMODULE to the main program, before IMPLICIT NONE.

0 Kudos
Lorenzo_W_
Beginner
662 Views

Steve, thank you for the answer.
If I add "USE MYMODULE", the compiler asks me to check the include path. Should I also add the path to where the .mod files are located?
I hoped to "link" only the library to my main program, not also all the *.mod files.

Thank you

0 Kudos
Arjen_Markus
Honored Contributor I
662 Views

The compiler needs to see the .mod files to be able to check the argument list and similar things. The linker needs to see the library (the object files). Those are two distinct steps in the build process. So, what you want is not possible ;).

0 Kudos
Steve_Lionel
Honored Contributor III
662 Views

The include path is where the compiler looks for the .mod files, so yes, you need to add that for the main program. If you have the library as a dependent project of the main one, this happens automatically.

0 Kudos
Reply