首页 > 解决方案 > Fortran 语言绑定错误:为什么不链接到 MPI 库?

问题描述

我正在尝试为消息传递接口创建语言绑定,但编译器无法识别我对 C 函数的引用...

我目前在 linux 机器上使用 gfortran 作为我的编译器...我知道语言绑定有效,但是每当我尝试将它们链接到 mpi 库时,我都会收到此错误:

    /tmp/ccwukrNT.o: In function `MAIN__':
    hello_init.f90:(.text+0x17): undefined reference to `mpi_init'
    collect2: error: ld returned 1 exit status

我目前使用的编译命令是:

    gfortran -ffree-form foo.f90 ~/dir1/dir2/bar.f90 -o <outfile> -L  
    /home/.fakeroot/lib -I /home/.fakeroot/include/ -lexampi

(exampi 是我目前正在研究的标准)

正如我所说,当我使用简单的 hello_world 程序并手动编译所有文件时,此方法有效,但当我尝试将其链接到库时它会失败。谁能帮我?

这是我当前对 MPI_Init 的 Fortran 绑定:

    module mpi_f08

    interface MPI_Init
    subroutine MPI_Init(ierror) bind(C)

    integer, optional, intent(out) :: ierror

    end subroutine MPI_Init
    end interface MPI_Init

    end module mpi_f08

我使用的 C 函数是微不足道的......它只是与 PMPI_Init() 接口......无论如何,库链接是主要问题......是的,我已经从 C++ 外部化了它。

这是我编写的简单测试程序:

    program hello_init
    use mpi_f08, only : MPI_Init
    implicit none
    integer :: ierror
    ierror = 0
    call MPI_Init(ierror)
    stop
    end program hello_init

我对 Fortran 不太熟悉......这是作为一个副项目给我的,但它很快就占据了我整个星期的时间,同时又笑又哭

谢谢!

标签: linkerfortranmpilinker-errorslanguage-binding

解决方案


推荐阅读