首页 > 解决方案 > 从 go 调用 fortran 库的最小示例

问题描述

我正在寻找这两种语言之间 FFI 的最小示例,一个非常简单的调用 Fortran 库的 Go 程序的 hello world。

我想强调一下,我不是在寻找外部资源、建议或教程,只是在 golang 中寻找最小的代码片段,在 Fortran 中寻找相应的格式。

这个网站上有很多例子:

Go -> Fortran 示例将与这些一致,并且对其他开发人员有用。

编辑以解决重复声明

这个问题有一个答案,而与之相关的那个是重复的,没有。关闭和重定向此问题作为可能以 -5 票关闭的重复项对 stackoverflow 用户没有用,尽管两者都提出了一个合理的问题。

标签: gofortranffi

解决方案


cgo(https://golang.org/cmd/cgo/)似乎提供了调用C的功能。所以我尝试使用它来调用Fortran(在OSX10.11上使用go-1.11 + gfortran-8.2),它似乎正在为这个简单的程序工作......

main.go:

package main

// #cgo LDFLAGS: mylib.o -L/usr/local/Cellar/gcc/8.2.0/lib/gcc/8 -lgfortran
// void hello();
// int  fort_mult( int );
// void array_test1 ( double*, int* );
// void array_test2_( double*, int* );
import "C"
import "fmt"

func main() {
    // print a message
    C.hello()

    // pass a value
    fmt.Println( "val = ", C.fort_mult( 10 ) )

    k := C.int( 777 )
    fmt.Println( "val = ", C.fort_mult( k ) )

    // pass an array
    a := []C.double {1, 2, 3, 4, 5.5555}
    n := C.int( len(a) )

    C.array_test1( &a[0], &n )  // pass addresses
    fmt.Println( a )

    C.array_test2_( &a[0], &n )  // no use of iso_c_binding
    fmt.Println( a )
}

mylib.f90:

subroutine hello() bind(C)
    print *, "Hello from Fortran"
end subroutine

function mult( x ) result( y ) bind(C,name="fort_mult")  ! can use a different name
    use iso_c_binding, only: c_int
    integer(c_int), value :: x
    integer(c_int) :: y

    y = x * 10
end function

subroutine array_test1( arr, n ) bind(C)   ! use iso_c_binding
    use iso_c_binding, only: c_int, c_double
    integer(c_int) :: n
    real(c_double) :: arr( n )

    arr(:) = arr(:) * 100.0d0
end subroutine

subroutine array_test2( arr, n ) ! no use of iso_c_binding (e.g. for legacy codes)
    integer :: n
    double precision :: arr( n )   ! or real(8) etc

    arr(:) = arr(:) * 2.0d0
end subroutine

编译:

gfortran -c mylib.f90
go build main.go
./main

结果:

 Hello from Fortran
val =  100
val =  7770
[100 200 300 400 555.5500000000001]
[200 400 600 800 1111.1000000000001]

推荐阅读