Fortran编译错误一例
login2.stampede(111)$ make
mpif90 -g -ffree-form -DTPV26 -O0 -c -ffixed-line-length-250 -o testInterfaceF.o -I../..//seism_io_interface-v0.2/include testInterface.f
ifort: command line warning #10006: ignoring unknown option '-ffree-form'
ifort: command line warning #10006: ignoring unknown option '-ffixed-line-length-250'
testInterface.f(1): error #5149: Illegal character in statement label field [p]
program main
^
testInterface.f(1): error #5149: Illegal character in statement label field [r]
program main
-^
testInterface.f(1): error #5149: Illegal character in statement label field [o]
program main
--^
testInterface.f(1): error #5149: Illegal character in statement label field [g]
program main
---^
testInterface.f(1): error #5149: Illegal character in statement label field [r]
program main
----^
testInterface.f(1): error #5118: First statement in file must not be continued
program main
-----^
testInterface.f(2): error #5149: Illegal character in statement label field [u]
use mpi
----^
testInterface.f(3): error #5149: Illegal character in statement label field [i]
implicit none
----^
解决办法:
mv testInterface.f testInterface.f90
字符串Fortran与C混合调用注意事项
C/FORTRAN 混合编程时,简单变量在两种语言中都有直接对应的类型,堆栈调用的规则也比较简单。
但字符串、字符串数组的传递可能是个特例。
由于不同的FORTRAN编译器,对字符串的堆栈解释不同,本文介绍的方法仅保证在gcc 与 gfortran环境下适用。
C调用Fortran例子
int main()
{
…….
char s[7];
int b[3];
…
cstrng_( s, &b[1], 7L );
…
}
Fortran中
SUBROUTINE cstrng(s, b2)
CHARACTER(*) s
INTEGER b2
END SUBROUTINE openfile
Fortran调用C例子
program main
use mpi
implicit none
…….
CHARACTER (LEN=7) :: s
integer :: b
…
cstrng( s, b);
…
end
C中
vois cstrng_(char *s, int *b2, unsigned slen)
{
char cs = (char)malloc(sizeof(char)*(slen+1));
strncpy(cs, s, slen);
strncpy(&cs[slen], “\0”, 1);
}