0

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 my(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 mysum(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

1 Answer 1

1

Simply, if you have a subroutine (or anything else) defined in a module that you want to be made accessible somewhere else you need to "use" that module:

program main
  use mymodule
  implicit none
  ....
end program

When you don't have that use mymodule which allows the compiler to see that the subroutine mysum is actually a module subroutine, the compiler is going to treat mysum still as an external subroutine.

Previously, mysum was an external subroutine, but that external subroutine was defined in the library object. It no longer is in the same way: there will likely be various bits of name mangling. It's your linker complaining.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I thought that "use mymodule" was not necessary in this case, since it's inside a library. This actually solves my issue, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.