I’m having trouble trying to compile a simple fortran program which uses a module in the same directory.
I have 2 files: test1.f90 which contains the program and modtest.f90 which contains the module.
This is test1.f90:
program test
use modtest
implicit none
print*,a
end program test
This is modtest.f90:
module modtest
implicit none
save
integer :: a = 1
end module modtest
Both files are in the same directory. I compile modtest.f90 and test.f90 like this:
gfortran -c modtest.f90
gfortran -o test1 test1.f90
But then I get this error:
/tmp/cckqu8c3.o: In function `MAIN__':
test1.f90:(.text+0x50): undefined reference to `__modtest_MOD_a'
collect2: ld returned 1 exit status
Is there something I’m missing?
Thanks for the help
What you’re doing is not telling the linker where reference module
modtestis so that the your code can use its contents.This should work:
Some context:
The
-ooption tells the compiler to put the output of the full build (compile + link) into a program calledtest1. Then we supply a file that we are to compile (test1.f90). Finally we are telling the compiler to consider a file that contains the compiled output of another build (modtest.o) and to link this to the compiled output oftest1.f90, and use the contents ofmodtest.owhen trying to sort out references within the test1.f90 that reference the modulemodtest(in the statementuse modtestin the source code).So the statement says:
Please compile and subsequently link
test1.f90tomodtest.o, and produce a file calledtest1as the final output.