Fortran 90 编译问题:未定义对的引用
我在尝试编译一个使用同一目录中的模块的简单 fortran 程序时遇到问题。 我有 2 个文件:包含程序的 test1.f90 和包含模块的 modtest.f90。
这是 test1.f90:
program test
use modtest
implicit none
print*,a
end program test
这是 modtest.f90:
module modtest
implicit none
save
integer :: a = 1
end module modtest
两个文件位于同一目录中。我像这样编译 modtest.f90 和 test.f90:
gfortran -c modtest.f90
gfortran -o test1 test1.f90
但后来我收到此错误:
/tmp/cckqu8c3.o: In function `MAIN__':
test1.f90:(.text+0x50): undefined reference to `__modtest_MOD_a'
collect2: ld returned 1 exit status
我缺少什么吗? 感谢您的帮助
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所做的并不是告诉链接器参考模块
modtest
的位置,以便您的代码可以使用其内容。这应该有效:
一些上下文:
-o
选项告诉编译器将完整构建(编译+链接)的输出放入名为test1
的程序中。然后我们提供一个要编译的文件(test1.f90
)。最后,我们告诉编译器考虑一个包含另一个构建的编译输出 (modtest.o
) 的文件,并将其链接到test1.f90
的编译输出,并在尝试整理 test1.f90 中引用模块modtest
的引用时使用modtest.o
的内容(在语句use modtest
在源代码中)。所以声明说:
请编译并随后将
test1.f90
链接到modtest.o
,并生成一个名为test1
的文件作为最终输出。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:
Some context:
The
-o
option 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.o
when trying to sort out references within the test1.f90 that reference the modulemodtest
(in the statementuse modtest
in the source code).So the statement says:
Please compile and subsequently link
test1.f90
tomodtest.o
, and produce a file calledtest1
as the final output.