9

I am trying to compile a fortran program which uses a bunch of modules. I obtain an error when I compile it, and it's driving me crazy. The error is originated by the addition of one subroutine and takes place when I try to recompile the program:

Main program contains these two lines:

--

call read_step(nStepOne,molOne)
call read_step(nStep,mol)

--

This is calling one of the subroutines in a file "fileio.f90":

--

subroutine read_step(n,tape)

implicit none

integer, intent(in) :: tape
integer, intent(out) :: n

character(len=6) :: dum

rewind(tape)
read (tape,*)
read (tape,*) dum, n
rewind(tape)
return
!
end subroutine read_step

--

When I try to compile it, the following error arises:

ifort -o SpIdMD.x *.o -static-intel -openmp 
SpIdMD.o: In function `MAIN__':
SpIdMD.f90:(.text+0x3b2): undefined reference to `read_step_'
SpIdMD.f90:(.text+0x3c5): undefined reference to `read_step_'
make: *** [SpIdMD.x] Error 1

Other calls to subroutines in the same module did not give any error, and I just don't see any difference between the calls to the "old subroutines" and the one I just created.

An example of one of these "old subroutines", which does not give any complaint, is:

In the main program:

call get_dim(n_atom,nSnap,mol)

In the fileio.f90:

subroutine get_dim(n,n_snap,tape)

implicit none

integer,intent(in) :: tape
integer,intent(out) :: n, n_snap
integer :: m

rewind(tape)
read (tape,*,err=1,end=2) n
rewind(tape)

m = 0
do while (.true.)
   read (tape,*,err=1,end=3)
   m = m +1
end do
3   n_snap = m/(n + 2)
if (m.ne.(n_snap*(n + 2))) stop  'unexpected end of input file'

rewind(tape)

return
!
1   stop 'error in input file'
2   stop 'unexpected end of input file'
end subroutine get_dim

I have absolutely no idea why this behavior. I'd be grateful if anybody could help me solve this nightmare. Thanks!

2
  • 2
    Have you run make clean and then tried make again ? Commented Apr 18, 2013 at 18:28
  • 1
    Yep, but doesn't solve anything. I made sure the fileio.o (containing the module) is updated. Commented Apr 18, 2013 at 21:45

2 Answers 2

10

If the definition of subroutine read_step is in a module, then you have either forgotten to add the USE statement for that module to the top of the main program, or the relevant procedures in the module are not PUBLIC.

With that compiler (and some others) linker names for module procedures generally consist of the module name followed by 'mp' (case may vary) followed by the procedure name, with various amounts of leading and trailing underscores salted to taste. Your linker error doesn't have any of that "mangling", which indicates that when compiling the scope with the procedure reference, the compiler doesn't think the procedure is a module procedure.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot IanH! You solved my problem. Thanks to your comment realised that had not added the name of the relevant subroutine to the "public" tag at the top of the module.
0

To be more concrete, I'll show how to use the USE and PUBLIC statements mentioned in the other answer.

I wrapped my F77 function like this:

  module mod
  contains
  FUNCTION FUNC(PARAM)
  ...
  END
  end module mod

While old code (1986) is upper case and my code is lower case. This compiles fine. You may add public func between module and contains. But this seems to be default so you don't need it.

When linking you need to pass your program and the library like this: gfortran -o prog prog.for mod.for (or .o if compiled before).

2 Comments

This should probably be a question, not an answer. When you compile the source with the module, an object file (.o) is generated. Are you providing this object file to the later linking step?
I know, I posted a question: stackoverflow.com/questions/32278178/… will remove this one, or update it to be a real answer. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.