简体   繁体   中英

ld error when running mpif90 on Mac OS X

I am attempting to compile a Fortran program that uses OpenMPI on OS X (10.11). I started off by installing gcc (5.2), gfortran (5.2), etc from Mac HPC . Then I downloaded the Open MPI source version 1.10.1 from the official site . I then built and installed Open MPI (configure, make, make install) and everything appeared to work. I did not receive any errors and the libraries and binaries are where I expect them to be.

I then proceeded to compile a very simple Open MPI fortran application using mpif90 and that's when I received the following linking error from 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

Has anyone ever seen this before? I suspect it has to do with the fact that I am not using the standard Apple build chain, but I'm not sure.

Code being compiled:

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

You have declared the variable f as a scalar double precision:

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

and then you reference f like this:

sum = sum + f(x)

This reference to f is interpreted as a function taking an argument x . This will compile fine, but the code you are presenting does not define a function f and the linking fails. To fix the issue you'll need to also compile and link with the file containing the code for the function f .

The easiest fix is to just include a function at end of your code. Add this after the end program line at the very end of your file:

!   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

This provides an implementation of f that matches the comment in your code and when included your code will successfully compile.

Check to see that the architecture of the binaries you downloaded really matches the Mac.

$ uname -a

Darwin MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64

and that the F90 config points to the correct header files. There should have been a configuration step in the instal. This looks to me like the F90 got the wrong path for ld and is looking for headers (*.h) files that aren't there or more likely are in a different place. Mac is not in agreement with other *NIXs as to where system files are. They might be in ~/Library rather than in places expected on other systems such as /usr/include. Be sure that you F90 really is for Mac OS X 11 and not for some generic *NIX system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM