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

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

问题描述

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

这是 test1.f90:

程序测试使用 modtest隐式无打印*,a结束程序测试

这是 modtest.f90:

模块 modtest隐式无保存整数 :: a = 1结束模块 modtest

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

gfortran -c modtest.f90gfortran -o test1 test1.f90

但后来我收到此错误:

/tmp/cckqu8c3.o: 在函数‘MAIN__’中:test1.f90:(.text+0x50):对`__modtest_MOD_a'的未定义引用collect2: ld 返回 1 个退出状态

有什么我遗漏的吗?感谢您的帮助

解决方案

您所做的不是告诉链接器引用模块 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 的文件作为最终输出.>

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

解决方案

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.

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

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