user3705273
user3705273

Reputation: 335

Error in fortran, undefined reference to subroutine

I am writing a subroutine and main function to call it, but getting error as undefined reference to ___. I found one reason: When I save the main and subroutine in the same file, compile and run that file, everything runs perfectly. However, when I save them into different .f90 files and try to run the main file, I get error. Is there any way I can make subroutine into a separate file and call into main calling program?

I got confused with another place - in the main program at !------ERROR------ place. I referred to Automatic width integer descriptor in fortran 90 I can use I0 as automatic width display indicator. But when I used the same, there is run time error expected integer but got character. Any idea about this?

! saved as sub_program.f90 file
SUBROUTINE sub_program (v1,v2,ctr)
IMPLICIT NONE
INTEGER, INTENT(IN) :: ctr
INTEGER, INTENT (OUT) :: v1,v2

SELECT CASE (ctr)
CASE (1)
v1=1
v2=0
CASE (2)
v1=0
v2=1
END SELECT

RETURN
END SUBROUTINE


! main calling program, saved as caller.f90
PROGRAM caller
IMPLICIT NONE
INTEGER :: v1,v2,ctr

    DO ctr = 1,2,1
        CALL sub_program (v1,v2,ctr)
        WRITE (*,100) 'STEP = ',ctr,'V1 = ',v1,'V2 = ',v2 !------ERROR------
        100 FORMAT (I0) 
    END DO

END PROGRAM     

Thanks!

Upvotes: 1

Views: 3234

Answers (1)

Raul Laasner
Raul Laasner

Reputation: 1585

  1. What is your compile command? For me, this compiles and runs normally

    gfortran caller.f90 foo.f90 && ./a.out
    
  2. I0 is an integer indicator, but some items following your WRITE statement are character strings. You can try, for example,

    100 FORMAT (3(A, I0, 1X))
    

    where 1X refers to a space.

As a note, if formatting is not terribly important and you're only interested in seeing some quick results, you can use the free format output (WRITE(*,*) ...).

EDIT: I had incorrectly referred to FORMAT as obsolete.

Upvotes: 2

Related Questions