Error(1147): This module has already been defined

Viewed 57

I write a code that employs a module from another code (I didn't write it). When I use this module with the main program, it works well and shows no error. When I use this module with the subroutine it shows:

This module has already been defined

tried to summarize the code properly. Here is reproducible code:

subroutine polygoneclipping(n,vtable0,vcoord0,BOUNDARYPOINTS0,NEWvcoord0,Unumber)

use SutherlandHodgmanUtil
 only : polygon,sutherlandHodgman,edgeClipping

type(polygon) :: p1, p2, res
integer :: c, n 
double precision, dimension(2) :: y1, y2
integer(kind =3) :: i, Unumber
integer(kind =3), dimension(40,1) :: vtable0
real(kind =3), dimension(40,2) :: VCOORD0
real(kind =3), dimension(4,2) :: BOUNDARYPOINTS0
real(kind =3), dimension(40,2) :: NEWvcoord0

!MAIN SUB PART

end subroutine

module SutherlandHodgmanUtil

type polygon
!type for polygons
! when you define a polygon, the first and the last vertices have to be the same
integer :: n
double precision, dimension(:,:), allocatable :: vertex
end type polygon

contains 

subroutine sutherlandHodgman( ref, clip, outputPolygon )

type(polygon) :: ref, clip, outputPolygon

type(polygon) :: workPolygon               ! polygon clipped step by step 
double precision, dimension(2) :: y1,y2    ! vertices of edge to clip workPolygon
integer :: i  

!MAIN SUB PART

end subroutine sutherlandHodgman

subroutine edgeClipping( poly, y1, y2, outputPoly )

type(polygon) :: poly, outputPoly
double precision, dimension(2) :: y1, y2, x1, x2, intersecPoint
integer ::  i, c

!MAIN SUB PART

end subroutine edgeClipping

function intersection( x1, x2, y1, y2)
double precision, dimension(2) :: x1, x2, &  ! points of the segment
                        y1, y2     ! points of the line

double precision, dimension(2) :: intersection, vx, vy, x1y1 
double precision :: a

!MAIN FUNC PART

end function intersection

function inside( p, y1, y2)
double precision, dimension(2) :: p, y1, y2, v1, v2
logical :: inside

!MAIN FUNC PART

end function inside

end module SutherlandHodgmanUtil

What is this error and why it appears?

I will happy with any help and forgive me for the long code.

1 Answers

If I run your code I get the following error message

a.f90:3:7:

    3 |   use SutherlandHodgmanUtil
      |       1
Fatal Error: Cannot open module file ‘sutherlandhodgmanutil.mod’ for reading at (1): No such file or directory

Explanation for that: In Fortran there are no automatic forward declarations. This means that the subroutine which tries to include sutherlandhodgmanutil fails as the module has not yet been defined.

Solutions:

  1. Define the subroutine after the module.
  2. Preferred way: Create a separate file for the module and compile the module first.

Your error message

This module has already been defined

probably results as you already have compiled the module SutherlandHodgmanUtil beforehand? Either from a different file or different test run? But that's just a wild guess..


By the way, I had to change your code as integer(kind=3) was not available on my machine. In general it is preferred to use machine independent kind defintions, e.g. check out use iso_fortran_env, only: int32, real32.

Related