使用链接库访问模块内的子例程
Access subroutine inside a module using a linked library
我想在不同的 Fortran 可执行文件中使用在静态库(用 Fortran 编写)中编写的子例程。
这是我的工作示例:
subroutine my(a,b,c)
implicit none
real*8, intent(in) :: a,b
real*8, intent(out) :: c
!
c = a + b
!
end subroutine
这会生成一个 XXX.lib 文件,我用这个 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
一切正常,但是当我想在模块中定义我的子例程时出现问题,如下所示:
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
此时 "main" 编译器无法找到子例程。有没有办法让编译器"see" 模块内部声明的子程序?
谢谢
简单地说,如果您在模块中定义了一个子例程(或其他任何东西),您希望在其他地方可以访问您需要 "use" 该模块:
program main
use mymodule
implicit none
....
end program
当你没有 use mymodule
让编译器看到子例程 mysum
实际上是一个模块子例程时,编译器将把 mysum
仍然视为一个外部子程序。
以前,mysum
是一个外部子程序,但那个外部子程序是在库对象中定义的。它不再以相同的方式出现:可能会有各种 name mangling。这是您的链接器在抱怨。
我想在不同的 Fortran 可执行文件中使用在静态库(用 Fortran 编写)中编写的子例程。 这是我的工作示例:
subroutine my(a,b,c)
implicit none
real*8, intent(in) :: a,b
real*8, intent(out) :: c
!
c = a + b
!
end subroutine
这会生成一个 XXX.lib 文件,我用这个 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
一切正常,但是当我想在模块中定义我的子例程时出现问题,如下所示:
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
此时 "main" 编译器无法找到子例程。有没有办法让编译器"see" 模块内部声明的子程序? 谢谢
简单地说,如果您在模块中定义了一个子例程(或其他任何东西),您希望在其他地方可以访问您需要 "use" 该模块:
program main
use mymodule
implicit none
....
end program
当你没有 use mymodule
让编译器看到子例程 mysum
实际上是一个模块子例程时,编译器将把 mysum
仍然视为一个外部子程序。
以前,mysum
是一个外部子程序,但那个外部子程序是在库对象中定义的。它不再以相同的方式出现:可能会有各种 name mangling。这是您的链接器在抱怨。