Fortran 90 编译问题:未定义对的引用

发布于 2024-09-27 02:20:35 字数 672 浏览 3 评论 0原文

我在尝试编译一个使用同一目录中的模块的简单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-10-04 02:20:35

您所做的并不是告诉链接器参考模块 modtest 的位置,以便您的代码可以使用其内容。

这应该有效:

gfortran -o test1 test1.f90 modtest.o

一些上下文:

-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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文