COMMON BLOCK
COMMON数据块是Fortran77语言中全局变量的唯一一种实现方式。程序单元(function & subroutine&program)之间可以引用同一个COMMON数据块里面的变量,实现全局变量共享。在一定规模的Fortran77工程中,会有多处COMMON数据块的身影,一般会把COMMON数据块定义放在一个包含文件里(.inc),需要用到的程序单元通过include语句把对应的包含文件引入进来。使用COMMON数据块要考虑成员对齐问题,另一个是最好加上SAVE属性,保证程序单元退出时成员变量的值有明确定义! Fortran90标准引入MODULE概念,可以完全取代Fortran77的COMMON数据块
在C语言中通过声明COMMON数据块对应的extern全局变量结构,可以引用Fortran中的COMMON数据块!
示例
common.inc
integer(c_int)::I,J,K
real(c_float)::R
common /IJKR/ I,J,K,R
save:: /IJKR/ !保证返回后 /IJKR/处于定义状态, 没有save属性,
!可能保存在寄存器中的变量被丢弃,不保证写出到全局变量空间
BIND(C,NAME="IJKR") :: /IJKR/ !需要和main里声明一致,不然会指向不同位置(gfortran)
fortran_comm_block.F90
!----------------------------------------------------------------
! common block等价于全局变量(global variables)
! 可以通过C进行访问
! 可以用module内变量取代common block, module 变量都具有save属性,
! 全局可见!
!----------------------------------------------------------------
subroutine cb_sub()
use iso_c_binding
implicit none
!引用common block /IJKR/
include 'common.inc'
print *,I,J,K,R
r=r+1.0
print *, r
return
end subroutine
program main
use iso_c_binding
implicit none
!引用 common block /IJKR/
include 'common.inc'
interface
subroutine c_print_IJKR() bind(C,NAME="print_IJKR")
end subroutine
end interface
I=10
J=20
k=30
r=1.1111
call cb_sub()
print*,r !标准并不保证cb_sub返回时common block是处于定义状态?
call c_print_IJKR()
stop
end program main
! https://www.ibm.com/docs/en/xl-fortran-linux/15.1.3?topic=attributes-save
! https://fluka-forum.web.cern.ch/t/how-to-access-fortran-common-blocks-from-c-c/202
! https://community.intel.com/t5/Intel-Fortran-Compiler/how-to-access-common-block-data-from-c/m-p/825877
!
common_block.c
#include <stdio.h>
//通过声明访问fortran的common block /IJKR/
//可能需要考虑类型对齐
extern struct {
int i,j,k;
float r;
}IJKR;
void print_IJKR()
{
printf("I=%d,J=%d,K=%d, R=%g\n",
IJKR.i,
IJKR.j,
IJKR.k,
IJKR.r
);
return;
}
编译
gcc -c common_block.c -o common_block.c.obj
gfortran -c fortran_common_block.F90
gfortran fortran_common_block.o common_block.c.obj
资料
FORTRAN 77 Reference: COMMON Blocks, BLOCK DATA and EQUIVALENCE
How to work with Fortran legacy code