编译和链接模块,与文件顺序无关 [英] Compilation and linking of modules independent of ordering in file
问题描述
我有一组用于 Main
fortran程序的模块.继承顺序为:
I have a set of modules for a Main
fortran program.
The inheritance order is:
user_input
> really_long_module
> useful_module
>主
user_input
> really_long_module
> useful_module
> Main
其中 user_input
是独立的,并且不会从任何其他模块继承.
where user_input
is independent and does not inherit from any other modules.
出于提高可用性的目的,我想将 really_long_module
移至文件末尾,因为该文件很少更新且非常长.尽管 useful_module
是用户经常进行更改的位置,所以感觉应该接近文件的开头是合理的.
For purposes of improved usability I would like to move really_long_module
to the end of the file because it is rarely updated and is very long. While useful_module
is where the user will frequently make changes, so it feels reasonable that this should be close to the beginning of the file.
我一直在阅读有关 make
文件以及如何使用它们指示依赖关系的信息.但是,我找不到任何描述单个文件中存在的模块相互依赖关系的 make
示例.
I have been reading about make
files and how they can be used to indicate dependencies. However, I have not been able to find any make
examples that describe the module inter-dependencies that exist within a single file.
我的问题:是否可以在单个fortran文件中以独立于模块依赖关系的顺序编写模块,并使用 make
文件描述编译期间的依赖关系?
My question: is it possible within a single fortran file, to write the modules in an order that is independent of their dependencies and use a make
file to describe the dependencies during compilation?
MWE:
module user_input
integer, PARAMETER :: N = 2
end module user_input
module really_long_module
use user_input, only: N
implicit none
real :: test_number = N
end module really_long_module
module useful_module
use user_input
use really_long_module
SAVE
real :: useful_output
END MODULE useful_module
PROGRAM Main
use user_input
use really_long_module
use useful_module
useful_output = 2 * N
print*, useful_output
END PROGRAM Main
! ### Would like to put really_long_module at the end of file
! module really_long_module
! use user_input, only: N
! implicit none
!
! real :: test_number = 0.5
!
! end module really_long_module
推荐答案
没有什么可以阻止编译器以任意顺序处理出现在文件中的模块的.但是,通常情况下,编译器在分析源文件时会按顺序工作,并且要求程序单元引用的所有模块都必须事先进行处理.
Nothing would prevent a compiler to work with modules appearing in a file in arbitrary order. However, it is usual that compilers work sequentially when analyzing a source file and require that all modules referred by a program unit would have been processed before.
请注意,该标准并未对程序单元的顺序施加任何限制,但是至少在2003年ISO/IEC FCD 1539-1:2004(E)草案中,该标准通过表述来认可了现行做法
Notice that the standard does not put any restriction on the program units order but, at least in the 2003 draft ISO/IEC FCD 1539-1:2004(E) it was recognizing the existing practice by stating that
本标准对构成程序的程序单元没有任何订购要求,但因为模块的公共部分需要在模块引用时可用在处理完之后,处理器可能需要对程序单元进行特定顺序的处理.
This standard places no ordering requirement on the program units that constitute a program, but because the public portions of a module are required to be available by the time a module reference is processed, a processor may require a particular order of processing of the program units.
这篇关于编译和链接模块,与文件顺序无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!