Fortran 90 模块的神秘本质 [英] The mysterious nature of Fortran 90 modules

查看:18
本文介绍了Fortran 90 模块的神秘本质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Fortran 90 模块是渐逝的生物.我使用了一段时间的(单一)模块并取得了一些成功(使用 Intel Visual Fortran 和 Visual Studio 2010 编译).然后我写了另一个模块并尝试在另一个函数中使用它,然后收到此错误:

Fortran 90 modules are evanescent creatures. I was using a (singular) module for a while with some success (compiling using Intel Visual Fortran and Visual Studio 2010). Then I wrote another module and tried to USE it in another function, before receiving this error:

 error #7002: Error in opening the compiled module file.  Check INCLUDE paths.

所以我删除了违规模块.但是现在我在尝试访问我的原始模块后收到同样的错误!

So I deleted the offending module. But now I receive the same error after when trying to access my original module!

我怎样才能找到这些神秘的生物?为什么一个模块可以工作,而两个模块不行?我假设我需要删除并重新编译它们,或者告诉编译器以某种方式包含它们.我知道源代码的文件位置,但不知道编译到哪里.

How can I locate these mysterious creatures? Why does one module work but not two? I'm assuming that I need to delete and recompile them, or tell the compiler to include them somehow. I know the file locations of the source code but not where they compile to.

推荐答案

对于那个特定的处理器(很多其他 Fortran 处理器也有类似的特性,但细节不同):

For that specific processor (many other Fortran processors have similar characteristics, but the details differ):

  • 成功编译模块后,编译器会生成一个 .mod 文件(可能还有 .obj 文件),其中包含有关模块提供的实体的信息.您引用的错误消息所指的正是这个 mod 文件.编译器在编译其他源时遇到模块的 USE 语句时需要此 mod 文件.(obj文件用于链接阶段.)

  • When a module is compiled successfully, the compiler generates a .mod file (and perhaps an .obj file) that contains information about the entities provided by the module. It is this mod file that the error message you quote is referring to. The compiler requires this mod file when it encounters a USE statement for the module while compiling other source. (The obj file is used in the link stage.)

因此,在使用模块之前,编译器必须在某个时间编译模块的源代码.这意味着模块的源代码 (MODULE...END MODULE) 必须在 USE 语句之前出现在源文件中,或者必须在使用 USE 语句在源文件之前编译的单独文件中.

Hence, before a module is USE'd, the compiler must at some time have compiled the source code for the module. That means that the module's source code (MODULE...END MODULE) must have appeared earlier in the source file prior to the USE statement, or must have been in a separate file that was compiled prior to the source file with the USE statement.

在 Visual Studio 中使用英特尔 Fortran 项目进行编译时,构建环境将自动尝试为项目中的源文件安排适当的编译顺序.从命令行使用 ifort 命令进行编译时,程序员负责管理编译顺序.

When compiling using an Intel Fortran project within Visual Studio, the build environment will automatically attempt to arrange an appropriate compilation order for the source files within a project. When compiling using the ifort command from the command line the programmer is responsible for managing the compilation order.

接收生成的 mod 文件的目录由提供给编译器的第一个/module 命令行选项指定.在 Visual Studio 中,此选项是使用 Fortran > 输出文件 > 模块路径属性设置的.默认情况下,Visual Studio 中的 Fortran 项目将此属性设置为当前配置的名称,因此 mod 文件出现在名为 Debug 或 Release 的项目的子目录中.在没有/module 命令行选项的情况下,mod 文件出现在当前目录中.

The directory that receives the generated mod files is specified by the first /module command line option given to the compiler. In Visual Studio this option is set using the Fortran > Output Files > Module Path property. By default, a Fortran project in Visual Studio has this property set to be the name of the current configuration, hence the mod files appear in a child directory of the project called Debug or Release. In the absence of a /module command line option the mod files appear in the current directory.

/module 命令行选项(或等效的 Visual Studio 属性)指定的目录也用于搜索 mod 文件.此外,还会搜索/I 命令行选项指定的目录(在 Visual Studio 中,Fortran > General > Additional Include Directories).

Directories specified by the /module command line option (or the equivalent Visual Studio property) are also used in the search for mod files. In addition, directories specified by the /I command line option (in Visual Studio, Fortran > General > Additional Include Directories) are searched.

从您的问题中不清楚您是如何在源文件中分配模块的,无论您有一个 Visual Studio 项目还是多个项目等.如果您只处理一个项目,那么通常所有这些都是需要的是将所有 Fortran 文件添加到项目的源文件中,默认设置应该有效".查找 mod 文件的错误可能是因为:

It is not clear from your question as to how you have distributed your modules amongst your source files, whether you have a single Visual Studio project or multiple projects etc. If you are only dealing with a single project then typically all that is required is to add all the Fortran files to the Source files for the project, and the default settings should "work". Errors in finding mod files may be because:

  • 模块的相关源代码不在项目的源文件之一中;

  • the associated source for the module isn't in one of the source files for the project;

模块的源代码编译由于其他原因失败(是否在构建序列的前面列出了其他错误?)

compilation of the source for a module failed for some other reason (are other errors listed earlier in the build sequence?)

模块是在特定源文件中使用后定义的;

a module is defined after its use in a particular source file;

模块之间存在循环依赖(模块 A 使用模块 B,而模块 B 使用 A 或类似模块——这是语言规则所不允许的);

there are circular dependencies between modules (module A uses module B which uses A or similar - this is not allowed by the rules of the language);

一些混淆构建顺序自动确定的源代码结构(旧版本的构建系统被带有双冒号的 F2003 形式的 use 语句混淆,另外有可能混淆 USE 语句,使得构建系统无法识别它们)但这些方面非常模糊.

some source construct that confuses the automatic determination of build order (older versions of the build system were confused by the F2003 form of use statement with the double colon, plus it is possible to obfuscate USE statements such that the build system fails to identify them) but these aspects are pretty obscure.

Visual Studio 中的多个 Fortran 项目可能需要修改依赖项目的模块搜索目录,以便他们可以在项目依赖树中找到以前项目编译的 mod 文件.如果 Visual Studio 中的项目间依赖项设置正确,更高版本的 Intel Fortran 也会自动处理此方面.

With multiple Fortran projects in Visual Studio there may be a need to modify the module search directories for dependent projects, such that they can find the mod files compiled by previous projects in the project dependency tree. Later versions of Intel Fortran handle this aspect automatically too, if the inter-project dependency settings in Visual Studio are correct.

这篇关于Fortran 90 模块的神秘本质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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