Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 链接到模块文件夹提供未定义的引用_Linux_Macos_Fortran_Ld - Fatal编程技术网

Linux 链接到模块文件夹提供未定义的引用

Linux 链接到模块文件夹提供未定义的引用,linux,macos,fortran,ld,Linux,Macos,Fortran,Ld,如果我正确理解了手册,它应该可以在文件夹中创建一个包含fortran模块的文件,比如/path/mods/test_mod.f90,即: 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 gfortran -I/path/mo

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

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
gfortran -I/path/mods -o test_prog test_prog.f90
要创建另一个文件,请说/path/bin/test_prog.f90,即:

program test_prog

use test_mod
real :: x,y,z

x=4e0
y=5e0
z=prod(x,y)
print*,z
end
并将其编译为:

gfortran -c test_mod.f90
gfortran -I/path/mods -o test_prog test_prog.f90
但出于某种原因,我在mac电脑上收到一个链接器错误,上面写着:

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
在Suse Linux上使用iPort进行相同的尝试,我得到:

/tmp/ifort2oZUKh.o: In function `MAIN__':
test_prog.f90:(.text+0x4d): undefined reference to `test_mod_mp_prod_'
有人能在我的黑暗中发光吗?谢谢 附言:把两者都写在一个文件里当然有效。在网上搜索时,我发现一些州的人(坦率地说,我只是不明白)说这可能与静态链接和动态链接有关。

gfortran -c test_mod.f90
应该生成两个文件:
test\u mod.mod
test\u mod.o
。你的另一份汇编声明

gfortran -I/path/mods -o test_prog test_prog.f90
正确指定查找
.mod
文件的位置,但忽略
.o
文件。
.mod
文件有点像编译器生成的头文件,它用于编译使用关联模块的任何程序单元,但链接需要目标文件

最简单的(我认为)解决办法是写

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

但是你可能想摆弄它。

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

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

这对我来说很有效。

嘿,已经非常感谢了!作为扩展,如果我在另一个程序中定义了一个模块,该模块本身也包含一个主要部分并包含相关对象,那么我当然会得到另一个链接器错误,该错误表示ld:duplicate symbol _main。除了把事情分开,还有别的办法吗?