链接到模块文件夹提供了未定义的参考 [英] Linking to modules folder gives undefined reference

查看:103
本文介绍了链接到模块文件夹提供了未定义的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正确理解了手册,它应该可以在文件夹中创建一个包含fortran模块的文件,例如/path/mods/test_mod.f90:

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

并像这样编译它:

gfortran -c test_mod.f90

并创建另一个文件,例如/path/bins/test_prog.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

并像这样编译它:

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

但是由于某种原因,我在Mac上收到链接器错误,说:

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

通过Ifort在Suse Linux上尝试相同的操作,我得到了:

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_'

有人可以在我的黑暗中发光吗?谢谢! PS .:将两者全部写入一个文件当然是可行的.在网上搜索时,我发现一些状态提示(坦率地说,我很不理解)说这可能与静态链接与动态链接有关.

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.

推荐答案

您还需要包括.o文件.也就是说,您应该将其编译为

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

这有效并为我运行.

这篇关于链接到模块文件夹提供了未定义的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆