1

I am trying to write two simple functions, one which contains the definition of a real function and the other one uses the previous function to find its derivative. However i keep getting the undefined reference error.

my module:

module module_name

    contains 

    function f(x)

        implicit none
        
        real(8)           :: x
        real(8)           :: f
    
        f= 2.71**x 

    endfunction

    function f_p_def(x)

        implicit none 

        real(8)           :: x, dx, f
        real(8)           :: f_p_def

        dx= 0.1

        f_p_def= (f(x+dx)-f(x))/dx
    
    endfunction 
endmodule 

then in my main program I have this:

program test 

use module_name

real(8)     :: f1, x

x=0

f1= f_p_def(x)

write(*,*) f1

endprogram 

and this is the error:

.\principal.o:principal.f95:(.text+0x27): undefined reference to `__module_name_MOD_f_p_def' collect2.exe: error: ld returned 1 exit status

Ian Bush
  • 6,996
  • 1
  • 21
  • 27

1 Answers1

2

Seems like you have your module defined in a separate file. This means that when producing the final executable you must add the object file as well.

EDIT To clarify, you need to compile each source file, producing an object file. Then at the end you link them together. The compiler driver program (gfortran in this case) can run the linker for you automatically.

Separating out all these steps, you need to do something like:

gfortran -O2 -g -fcheck=all -c principal.f95
gfortran -O2 -g -fcheck=all -c module_name.f95
gfortran -O2 -g -fcheck=all principal.o module_name.o -o myprogram
./myprogram

where module_name.f95 is the name of the file where module_name is defined. If not, modify appropriately. In the above, the first two commands compile the source files, producing object files. The third command doesn't actually compile anything, but rather links together your two object files, producing the executable. Then the final command executes the executable.

janneb
  • 36,249
  • 2
  • 81
  • 97
  • Yes the module is in a seperate file. principal.f95 is where the main program is and module_name.f95 is where the module module_name is. How do i run that command exactly ? I always do gfortran -c prinpal.f95 then gfortran -o prog principal.o and then prog.exe to execute the program and it works. I typed what you put in and it gave me module_name.o no such file or directory. – BoringLengthiness May 14 '21 at 12:59
  • @OmarHamze see https://stackoverflow.com/a/67534897/721644 In the above aswer you obviously have to change "module_name" to "principal". So `principal.o`. – Vladimir F Героям слава May 14 '21 at 13:09
  • Hello, I did what you said and what was in the link you sent, error basically the same: module.o:module.f95:(.text+0x37): undefined reference to `f_' module.o:module.f95:(.text+0x44): undefined reference to `f_' collect2.exe: error: ld returned 1 exit status – BoringLengthiness May 14 '21 at 13:36
  • @OmarHamze That is a new different error. Delete `f` from ` real(8) :: x, dx, f` inside `f_p_def`. We have questions and answers about this kind of error. See https://stackoverflow.com/questions/23992889/undefined-reference-to-procedure-defined-in-the-same-module and follow the links there. – Vladimir F Героям слава May 14 '21 at 14:07
  • @OmarHamze: I clarified my answer, HTH. – janneb May 14 '21 at 15:02