Fortran 共享对象的分段错误
Segmentation Fault from Fortran shared object
我在文件 gfunc.f90
中有一个 fortran 子例程。我想从 test.f90
.
中的主程序中调用这个子例程
如果我将两个文件放在同一个目录中并用
编译它们
gfortran gfunc.f90 test.f90 -o test
它工作正常。
但我希望子程序在库中。因此,我创建了一个名为 gfunc/
的子文件夹,并将 gfunc.f90
放在那里。在该文件夹中,我使用
编译模块
gfortran -fdefault-real-8 -fPIC -c gfunc.f90
和
gfortran -fdefault-real-8 -shared -o gfunc.so gfunc.o
我现在用
编译主程序
gfortran test.f90 gfunc/gfunc.so
但是现在只要访问子例程中的变量,我就会遇到分段错误。
我该如何正确编译和 link 库?
在这里,您可以找到一个最小的工作示例来重现问题:
gfunc.f90
:
module gfunc_module
implicit none
contains
subroutine gfunc(x, n, m, a, b, c)
double precision, intent(in) :: x
integer, intent(in) :: n, m
double precision, dimension(n), intent(in) :: a
double precision, dimension(m), intent(in) :: b
double precision, dimension(n, m), intent(out) :: c
integer :: i, j
do j=1,m
do i=1,n
c(i,j) = exp(-x * (a(i)**2 + b(j)**2))
end do
end do
end subroutine
end module
test.f90
:
program main
use gfunc_module
implicit none
integer, parameter :: dp = kind(1.0d0)
real(dp) :: x = 1.
integer, parameter :: n = 4
integer, parameter :: m = 4
real(dp), dimension(n) :: a = [-1., -0.33333333, .033333333, 1.]
real(dp), dimension(m) :: b = [-1., -0.33333333, .033333333, 1.]
real(dp), dimension(n, m) :: c
call gfunc(x, n, m, a, b, c)
write(*,*) c
end program main
您应该将 fdefault-real-8
添加到您的 test.f90 编译中:
gfortran -fdefault-real-8 test.f90 gfunc/gfunc.so
来自 documentation for gfortran -fdefault-real-8
使 DOUBLE PRECISION
变量为 16 字节宽,因此在 test.f90 上没有它,双精度变量只有 8 字节宽:
-fdefault-real-8
Set the default real type to an 8 byte wide type. Do nothing if this is already the default. This option also affects the kind of non-double real constants like 1.0, and does promote the default width of "DOUBLE PRECISION" to 16 bytes if possible, unless "-fdefault-double-8" is given, too.
我在文件 gfunc.f90
中有一个 fortran 子例程。我想从 test.f90
.
如果我将两个文件放在同一个目录中并用
编译它们gfortran gfunc.f90 test.f90 -o test
它工作正常。
但我希望子程序在库中。因此,我创建了一个名为 gfunc/
的子文件夹,并将 gfunc.f90
放在那里。在该文件夹中,我使用
gfortran -fdefault-real-8 -fPIC -c gfunc.f90
和
gfortran -fdefault-real-8 -shared -o gfunc.so gfunc.o
我现在用
编译主程序gfortran test.f90 gfunc/gfunc.so
但是现在只要访问子例程中的变量,我就会遇到分段错误。
我该如何正确编译和 link 库?
在这里,您可以找到一个最小的工作示例来重现问题:
gfunc.f90
:
module gfunc_module
implicit none
contains
subroutine gfunc(x, n, m, a, b, c)
double precision, intent(in) :: x
integer, intent(in) :: n, m
double precision, dimension(n), intent(in) :: a
double precision, dimension(m), intent(in) :: b
double precision, dimension(n, m), intent(out) :: c
integer :: i, j
do j=1,m
do i=1,n
c(i,j) = exp(-x * (a(i)**2 + b(j)**2))
end do
end do
end subroutine
end module
test.f90
:
program main
use gfunc_module
implicit none
integer, parameter :: dp = kind(1.0d0)
real(dp) :: x = 1.
integer, parameter :: n = 4
integer, parameter :: m = 4
real(dp), dimension(n) :: a = [-1., -0.33333333, .033333333, 1.]
real(dp), dimension(m) :: b = [-1., -0.33333333, .033333333, 1.]
real(dp), dimension(n, m) :: c
call gfunc(x, n, m, a, b, c)
write(*,*) c
end program main
您应该将 fdefault-real-8
添加到您的 test.f90 编译中:
gfortran -fdefault-real-8 test.f90 gfunc/gfunc.so
来自 documentation for gfortran -fdefault-real-8
使 DOUBLE PRECISION
变量为 16 字节宽,因此在 test.f90 上没有它,双精度变量只有 8 字节宽:
-fdefault-real-8
Set the default real type to an 8 byte wide type. Do nothing if this is already the default. This option also affects the kind of non-double real constants like 1.0, and does promote the default width of "DOUBLE PRECISION" to 16 bytes if possible, unless "-fdefault-double-8" is given, too.