Fortran 90 编译问题:对 <modulename> 的未定义引用 [英] Fortran 90 compiling issue: undefined reference to <modulename>

查看:36
本文介绍了Fortran 90 编译问题:对 <modulename> 的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试编译使用同一目录中的模块的简单 fortran 程序时遇到问题.我有 2 个文件:包含程序的 test1.f90 和包含模块的 modtest.f90.

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.

这是test1.f90:

This is test1.f90:

program test
  use modtest
  implicit none
  print*,a
end program test

这是 modtest.f90:

This is modtest.f90:

module modtest
  implicit none
  save
  integer :: a = 1
end module modtest

两个文件都在同一个目录中.我像这样编译 modtest.f90 和 test.f90:

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

然后我得到这个错误:

/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

推荐答案

你所做的并不是告诉链接器引用模块 modtest 在哪里,以便你的代码可以使用它的内容.

What you're doing is not telling the linker where reference module modtest is so that the your code can use its contents.

这应该可行:

gfortran -o test1 test1.f90 modtest.o

一些上下文:

-o 选项告诉编译器将完整构建(编译 + 链接)的输出放入名为 test1 的程序中.然后我们提供一个我们要编译的文件 (test1.f90).最后,我们告诉编译器考虑一个包含另一个构建 (modtest.o) 的编译输出的文件,并将其链接到 test1.f90 的编译输出,并在尝试整理 test1.f90 中引用模块 modtest 的引用时使用 modtest.o 的内容(在语句 use modtest 在源代码中).

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).

所以声明说:

请编译并随后将 test1.f90 链接到 modtest.o,并生成一个名为 test1 的文件作为最终输出.

Please compile and subsequently link test1.f90 to modtest.o, and produce a file called test1 as the final output.

这篇关于Fortran 90 编译问题:对 <modulename> 的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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