-1

I'm relatively new to fortran90 that I need for a project.

I have three scripts, 2 modules and the main program with the following structure:

script 1:

program main
   use module1
   implicit none
   ..
   call sub_from_mod1
end program main

script 2:

module module_2
   implicit none
   contains
   ..
end module module_2

script 3:

module module_1
   use module_2
   implicit none
   contains 
   ...
   subroutine sub_from_mod1
   ...
end module module_1

When compiling in CodeBlocks the whole project, I get an error:

undefined reference to sub_from_mod1_

Does anyone know what is the case?

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Sp_z
  • 1

1 Answers1

1

There is a typo in the use statement of the main program: correct would be

use module_1 !not use module1

Placing the main file in the file main.f90 and the modules in mod1.f90 and mod2.f90 I can compile the code with:

gfortran -c mod2.f90
gfortran -c mod1.f90
gfortran -c main.f90
gfortran main.o mod1.o mod2.o

The -c flag on the first three lines tells the compiler just compile and not link the code. This is necessary because the .mod files need to exist when the program is linked on the last line.

nameiki
  • 138
  • 9