Topic: undefined reference to a subroutine

Just installed Simply Fortran on Windows11, tried to build and run a simple program

program test
implicit none

Double Precision s(10)
INTEGER IFAIL

Call G05FEF(0.5D0,0.5D0,10,s,IFAIL)

print *, "s is", s

end program test

that calls some functions and routines, which I added to the project outline. Project building  looks fine, with message Generating Makefile ... Okay, * Complete*, but when I compile and run the program file, I get the message undefined reference to 'g05fef_'. Any help is greatly appreciated.

Re: undefined reference to a subroutine

The call to G05FEF is indeed undefined. When you declare implicit none, the Fortran compiler will not make any assumptions about anything, especially the signature of a subroutine.  You need to do something such that the Fortran compiler knows what G05FEF is.

The more modern technique would be to place G05FEF in a module:

module my_subroutines
implicit none

    contains

    subroutine G05FEF(a, b, i, s, fail_flag)
        ! body of G05FEF
        ! ...
    end subroutine G05FEF

end module my_subroutines

and then your program would just be:

program test
use my_subroutines
implicit none

Double Precision s(10)
INTEGER IFAIL

Call G05FEF(0.5D0,0.5D0,10,s,IFAIL)

print *, "s is", s

end program test

If G05FEF is already in a module, make sure you add the module to your program with a USE statement.

Alternatively, you can create an interface block to define the subroutine in your main program:

program test
implicit none

Double Precision s(10)
INTEGER IFAIL

interface
    subroutine G05FEF(a, b, i, s, fail_flag)
    real(kind=8)::a, b
    integer::i, fail_flag
    real(kind=8), dimension(*)::s
end interface

Call G05FEF(0.5D0,0.5D0,10,s,IFAIL)

print *, "s is", s

end program test

It can be a bit messier, but it allows using legacy routines that aren't inside a module.

Jeff Armstrong
Approximatrix, LLC

Re: undefined reference to a subroutine

Thanks. It seems that my problems are just compounding on top of each other. I put the subroutine in question along with 9 or 10 of its dependencies in a module but the main program won't compile, complaining that File 'my_subroutines.mod' opened at (1) is not a GNU Fortran module file. These are subroutines from my old nag library. I'll keep trying.

Re: undefined reference to a subroutine

Did you put them directly in a file named my_subroutines.mod? Module files (.mod) are output by the compiler when it encounters a module statement in a Fortran source file (.f90, etc.).  You should never be manually creating a .mod file.  The should just be in a normal Fortran source file, perhaps my_subroutines.f90.

Jeff Armstrong
Approximatrix, LLC

Re: undefined reference to a subroutine

yes, and tried the f90 extension, some incompatible issues with syntax erros, invalid characters, etc... from those subroutines. Thanks.