【问题标题】:ld error when running mpif90 on Mac OS X在 Mac OS X 上运行 mpif90 时出现 ld 错误
【发布时间】:2016-02-09 12:55:26
【问题描述】:

我正在尝试在 OS X (10.11) 上编译使用 OpenMPI 的 Fortran 程序。我首先从Mac HPC 安装 gcc (5.2)、gfortran (5.2) 等。然后我从the official site 下载了 Open MPI 源版本 1.10.1。然后我构建并安装了 Open MPI(配置、制作、制作安装),一切似乎都可以正常工作。我没有收到任何错误,并且库和二进制文件在我期望的位置。

然后我开始使用 mpif90 编译一个非常简单的 Open MPI fortran 应用程序,这时我收到了来自 ld 的以下链接错误。

Undefined symbols for architecture x86_64:
"_f_", referenced from:
    _MAIN__ in ccm61Nim.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

有人见过这个吗?我怀疑这与我没有使用标准的 Apple 构建链有关,但我不确定。

正在编译的代码:

program main
    use mpi
    double precision    PI25DT
    parameter           (PI25DT = 3.141592653689793238462643d0)
    double precision    mypi, pi, h, sum, x, f, a
    integer             n, myid, numprocs, i, ierr

!   function to integrate f(a) = 4.d0 / (1.d0 + a*a)


    call MPI_INIT(ierr)
    call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)
    call MPI_COMM_SIZE(MPI_COMM_WORLD, numprocs, ierr)

    do
        if (myid .eq. 0) then
            print *, 'Enter the number of intervals: (0 quits)'
            read(*,*) n
        endif
!   broadcast n
        call MPI_BCAST(n, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, ierr)
!   check for quit signal
        if (n .le. 0) exit
!   calculate the interval size
        h   = 1.0d0 / n
        sum = 0.0d0
        do i = myid + 1, n, numprocs
            x = h * (dble(i) - 0.5d0)
            sum = sum + f(x)
        enddo
        mypi = h * sum

!   collect all the partial sums
        call MPI_REDUCE(mypi, pi, 1, MPI_DOUBLE_PRECISION, MPI_SUM, 0, MPI_COMM_WORLD, ierr)

! node 0 prints the answer

        if (myid .eq. 0) then
            print *, 'pi is ', pi, ' Error is', abs(pi - PI25DT)
        endif
    enddo
    call MPI_FINALIZE(ierr)
end

【问题讨论】:

    标签: macos fortran fortran90 openmpi


    【解决方案1】:

    您已将变量 f 声明为双精度标量:

    double precision    mypi, pi, h, sum, x, f, a
    

    然后你像这样引用f

    sum = sum + f(x)
    

    f 的引用被解释为采用参数x 的函数。这将编译得很好,但是您提供的代码没有定义函数 f 并且链接失败。要解决此问题,您还需要编译并链接包含函数f 的代码的文件。

    最简单的解决方法是在代码末尾包含一个函数。在文件最后的end program 行之后添加:

    !   function to integrate f(a) = 4.d0 / (1.d0 + a*a)
    double precision function f(a)
      implicit none
      double precision :: a
      f = 4.d0 / (1.d0 + a*a)
    end function f
    

    这提供了f 的实现,它与您代码中的注释相匹配,并且当包含您的代码时,您的代码将成功编译。

    【讨论】:

      【解决方案2】:

      检查您下载的二进制文件的架构是否与 Mac 真正匹配。

      $ uname -a

      Darwin MacBook-Pro.local 15.0.0 达尔文内核版本 15.0.0:2015 年 9 月 19 日星期六 15:53:46 PDT;根:xnu-3247.10.11~1/RELEASE_X86_64 x86_64

      并且 F90 配置指向正确的头文件。应该有配置 安装步骤。在我看来,F90 的 ld 路径错误,并且正在寻找不存在或更可能位于不同位置的标头 (*.h) 文件。 Mac 与其他 *NIX 不同意系统文件的位置。它们可能在 ~/Library 中,而不是在其他系统上预期的位置,例如 /usr/include。请确保您的 F90 确实适用于 Mac OS X 11,而不适用于某些通用 *NIX 系统。

      【讨论】:

        最近更新 更多