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

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

问题描述

Fortran 90模块是e灭的生物.我使用(单一)模块已经有一段时间了,并且取得了一些成功(使用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中使用Intel 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>常规>其他包含目录).

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天全站免登陆