Fortran 90 compiling issue: undefined reference to <modulename>
What you're doing is not telling the linker where reference module modtest is so that the your code can use its contents.
This should work:
gfortran -o test1 test1.f90 modtest.o
Some context:
The -o option tells the compiler to put the output of the full build (compile + link) into a program called test1. 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 of test1.f90, and use the contents of modtest.o when trying to sort out references within the test1.f90 that reference the module modtest (in the statement use modtest in the source code).
So the statement says:
Please compile and subsequently link test1.f90 to modtest.o, and produce a file called test1 as the final output.
Related videos on Youtube
Eddy
Updated on March 29, 2020Comments
-
Eddy over 2 yearsI'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 testThis is modtest.f90:
module modtest implicit none save integer :: a = 1 end module modtestBoth files are in the same directory. I compile modtest.f90 and test.f90 like this:
gfortran -c modtest.f90 gfortran -o test1 test1.f90But 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 statusIs there something I'm missing? Thanks for the help
-
M. S. B. over 9 yearsOr:gfortran modtest.f90 test1.f90 -o test1
-
-
Eddy about 12 yearsYes it does, thanks! I've been learning Fortran for a month now, I can't believe I didn't know that :P -
Vladimir F Героям слава almost 7 yearsWhile this command does solve the problem, some description should be provided. I will be happy to retract my downvote once the answer is improved. -
Bruce Becker over 6 yearsThe solution is not enough - please provide some context as to why this solves the problem. -
user32882 over 5 yearsI used this as a basis to compile a much larger project but am getting the same problem. Will this work for programs using modules that use other modules? Please check out stackoverflow.com/questions/44870715/… -
Preet Sangha over 5 yearsYou would have to look that the options for gfortran and see if there are extra options you need.