未定义的对模块中函数的引用

时间:2016-11-30 08:13:50

标签: module fortran undefined fortran90 subroutine

我正在浏览关于未定义引用的所有其他线程,我无法弄清楚为什么这段代码不起作用。我基本上试图从一个模块运行一个函数到主程序中的子程序。但我一直收到错误

主:

    program main
        use m1
        implicit none
        integer, dimension(:,:), allocatable :: A,AFun
        integer :: n,i,j
        print *, "enter value for n"
        read *, n
        do i=1,n
                do j=1,n
                        A(i,j)=i**2-j**3
                end do
        end do
        print *, "Matrix A: "
        call prMat(A,n)
        call matFun(A,AFun,n)
        print *, "Matrix AFun:"
        call prMat(AFun,n)
        call fromAvg(AFun,n)

        contains
        subroutine prMat(x,n)
                implicit none
                integer, dimension(n,n) :: x
                integer :: i,j,n
                do i=1,n
                        write(*,*), (x(i,j))
                end do
        end subroutine prMat


        subroutine matfun(x,y,n)
                implicit none
                integer, dimension(n,n) :: x,y
                integer :: i,j,n,f1
                do i = 1,n
                        do j=1,n
                                y(i,j)=f1(x,i,j,n)
                        end do
                end do

        end subroutine matFun
    subroutine fromAvg(x,n)

            integer, dimension(:,:) :: x
            integer :: i,j,n
            integer :: s,avg,g,b
            s=0; g=0; b=0
            do i=1,n
                    do j=1,n
                            s=s+x(i,j)
                    end do
            end do
            avg=s/(n*n)

            do i = 1,n
                    do j = 1,n
                            if ( x(i,j) > avg ) then
                                    g =  g + 1
                            else
                                    b = b + 1
                            end if
                    end do
            end do


            print *, "In from avg, average=", avg
            print *, "Number of values greater than average is ",g
            print *, "Number of values less than average is ",b
    end subroutine fromAvg

        integer, dimension(:,:), allocatable, intent(IN) :: x
        integer :: i,j,n
        integer :: s,avg,g,b
        s=0; g=0; b=0
        do i=1,n
                do j=1,n
                        s=s+x(i,j)
                end do
        end do
        avg=s/(n*n)

        do i = 1,n
                do j = 1,n
                        if ( x(i,j) > avg ) then
                                g =  g + 1
                        elseif ( x(i,j) < avg ) then
                                b = b + 1
                        end if
                end do
        end do


        print *, "In from avg, average=", avg
        print *, "Number of values greater than average is ",g
        print *, "Number of values less than average is ",b
end subroutine fromAvg

end program main

模块功能:

module m1
implicit none
private
public :: f1
contains
function f1(x,p,q,n)
        integer, dimension(:,:) :: x
        integer, intent(in) :: p,q,n
        integer :: i,f1
        f1=0
        do i = 1,n
                f1 = f1 + x(p,n)
        end do
        do i = 1,n
                f1 = f1 + x(n,q)
        end do

end function f1
end module m1

我一直得到的错误如下:

/tmp/ccKZHw7L.o: In function `matfun.1520':
lab4_b.f90:(.text+0x808): undefined reference to `__m1_MOD_f1'
collect2: ld returned 1 exit status

我错过了什么吗?我在use m1语句之前的主程序开头有implicit none

1 个答案:

答案 0 :(得分:1)

在子程序中matfun f1被(重新)声明为整数,在行

        integer :: i,j,n,f1

这有效地掩盖了模块功能。由于代码是(正确地)使用包含f1的模块,因此它也不应该在子例程中声明。

然后我的编译器抱怨fromavg没有定义,但我想你知道。