Linking to modules folder gives undefined referenc

2020-04-18 03:12发布

if I understood the manual correctly it should work to creat a file containing a fortran module in a folder, say /path/mods/test_mod.f90 i.e. :

module test_mod
implicit none
save

contains

function prod(a,b) result(c)
real :: a,b,c

c=a*b
return
end function
end module

and compile it like:

gfortran -c test_mod.f90

And to create another file, say /path/bins/test_prog.f90, i.e:

program test_prog

use test_mod
real :: x,y,z

x=4e0
y=5e0
z=prod(x,y)
print*,z
end

and to compile it like:

gfortran -I/path/mods -o test_prog test_prog.f90

But for some reason I get a linker error on the mac saying:

Undefined symbols for architecture x86_64:
  "___test_mod_MOD_prod", referenced from:
      _MAIN__ in ccz1rsxY.o
ld: symbol(s) not found for architecture x86_64
collect2: ld gab 1 als Ende-Status zurück

Trying the same on a Suse Linux with Ifort I get:

/tmp/ifort2oZUKh.o: In function `MAIN__':
test_prog.f90:(.text+0x4d): undefined reference to `test_mod_mp_prod_'

Could someone please shine light in my darkness? Thanks! PS.: Writing both in one file of course works. Searching on the web I found some statemenst (that I quite frankly just not understood) saying that this might be related to static vs. dynamic linking.

2条回答
我只想做你的唯一
2楼-- · 2020-04-18 03:49

You need to include the .o file as well. That is, you should compile this as

gfortran -I/path/to/mods -o test_prog test_prog.f90 mods/test_mod.o

This worked and ran for me.

查看更多
等我变得足够好
3楼-- · 2020-04-18 04:03

This

gfortran -c test_mod.f90

should produce two files: test_mod.mod and test_mod.o. Your other compilation statement

gfortran -I/path/mods -o test_prog test_prog.f90

correctly specifies the location in which to look for the .mod file but omits the .o file. The .mod file is a bit like a compiler-generated header file, it is used for compilation of any program units which use-associate the module, but the object file is needed for linking.

The simplest (I think) fix is to write

gfortran -o test_prog -I/path/mods /path/mods/test_mod.o test_prog.f90

but you may want to fiddle around with that.

查看更多
登录 后发表回答