From: guoguo on
A module variable can not interoperate with a C global variable when I
use intel fortran 11.1.065 or 11.1.053
the source code:
C language:
-----------------------------------------------------------------------------------------------
#include <stdio.h>

int entier_externe;
double double_externe;

extern "C" void sp( float m1[6][3], double m2[][5], int *n, char *ch )
{
int i, j;

printf( "\t\t%s\n\n", ch );
printf( "entier_externe = %d\n", entier_externe );
printf( "double_externe = %f\n", double_externe );
printf( "\t\tMatrices C\n\n" );
for( j=0; j<3; j++ ) {
for( i=0; i<6; i++ )
printf( "%f ", m1[i][j] );
printf( "\n" );
}
printf( "\n" );
for( j=0; j<5; j++ ) {
for( i=0; i<*n; i++ )
printf( "%f ", m2[i][j] );
printf( "\n" );
}
return;
}
-----------------------------------------------------------------------------------------------
fortran :
-----------------------------------------------------------------------------------------------
module m
use ISO_C_BINDING
interface
subroutine sp( mat1, mat2, n, chaine ) bind(C)
import C_FLOAT, C_DOUBLE, C_INT, C_CHAR
real(kind=C_FLOAT), dimension(3,6) :: mat1
real(kind=C_DOUBLE), dimension(5,*) :: mat2
integer(kind=C_INT) :: n
character(kind=C_CHAR), dimension(*) :: chaine
end subroutine sp
end interface

integer(kind=C_INT), bind(C,name='entier_externe') :: entier_externe
real(kind=C_DOUBLE), bind(C,name='double_externe') :: double_externe
end module m

program prog
use m
use ISO_FORTRAN_ENV
real(kind=C_FLOAT), dimension(3,6) :: m1
real(kind=C_DOUBLE), dimension(5,7) :: m2
character(kind=C_CHAR,len=255) :: chaine = &
"Interoperabilite Fortran/C"//C_NULL_CHAR
integer i

entier_externe = 1756
double_externe = acos( -1._C_DOUBLE )
hu = 12345
call random_number( m1 )
call random_number( m2 )
call sp( m1, m2, 7_C_INT, chaine )
write( OUTPUT_UNIT, '(/, 2a, "Matrices Fortran 2003",/)' )
C_HORIZONTAL_TAB, &

C_HORIZONTAL_TAB
do i=1, size(m1,1)
write( OUTPUT_UNIT, '(6f9.6)' ) m1(i,:)
end do
print *
do i=1, size(m2,1)
write( OUTPUT_UNIT, '(7f9.6)' ) m2(i,:)
end do
end program prog
-----------------------------------------------------------------------------------------------

gfortran 4.5 can compile them well, and run correctly;
the result:
-----------------------------------------------------------------------------------------------
Interoperabilite Fortran/C

entier_externe = 1756
double_externe = 3.141593
Matrices C

0.997560 0.747928 0.073754 0.342244 0.900524 0.661932
0.566825 0.367391 0.005355 0.217952 0.386766 0.016108
0.965915 0.480637 0.347081 0.133160 0.445482 0.650855

0.646409 0.968539 0.100383 0.658229 0.256798 0.901923 0.147835
0.322987 0.598400 0.755453 0.150717 0.550865 0.657925 0.674529
0.855692 0.672981 0.605693 0.612315 0.659048 0.728859 0.769614
0.401287 0.456882 0.719048 0.978660 0.554005 0.402455 0.339323
0.206874 0.330015 0.897335 0.999142 0.977760 0.928628 0.115819

Matrices Fortran 2003

0.997560 0.747928 0.073754 0.342244 0.900524 0.661932
0.566825 0.367391 0.005355 0.217952 0.386766 0.016108
0.965915 0.480637 0.347081 0.133160 0.445482 0.650855

0.646409 0.968539 0.100383 0.658229 0.256798 0.901923 0.147835
0.322987 0.598400 0.755453 0.150717 0.550865 0.657925 0.674529
0.855692 0.672981 0.605693 0.612315 0.659048 0.728859 0.769614
0.401287 0.456882 0.719048 0.978660 0.554005 0.402455 0.339323
0.206874 0.330015 0.897335 0.999142 0.977760 0.928628 0.115819

but intel fortran run:
-----------------------------------------------------------------------------------------------
Interoperabilite Fortran/C

entier_externe = 0
double_externe = 0.000000
Matrices C

0.000000 0.666914 0.335355 0.832693 0.089918 0.734553
0.025480 0.963055 0.915327 0.345043 0.888284 0.300176
0.352516 0.838288 0.795864 0.871184 0.700979 0.049718

0.908189 0.926452 0.852292 0.169864 0.539018 0.585771 0.438701
0.097659 0.075641 0.121078 0.994743 0.480063 0.767068 0.208753
0.040313 0.911790 0.994575 0.753692 0.839226 0.444817 0.731112
0.085025 0.091822 0.778250 0.217974 0.025420 0.709505 0.665731
0.558821 0.637766 0.019665 0.659293 0.989383 0.794526 0.722074

Matrices Fortran 2003

0.000000 0.666914 0.335355 0.832693 0.089918 0.734553
0.025480 0.963055 0.915327 0.345043 0.888284 0.300176
0.352516 0.838288 0.795864 0.871184 0.700979 0.049718

0.908189 0.926452 0.852292 0.169864 0.539018 0.585771 0.438701
0.097659 0.075641 0.121078 0.994743 0.480063 0.767068 0.208753
0.040313 0.911790 0.994575 0.753692 0.839226 0.444817 0.731112
0.085025 0.091822 0.778250 0.217974 0.025420 0.709505 0.665731
0.558821 0.637766 0.019665 0.659293 0.989383 0.794526 0.722074
-----------------------------------------------------------------------------------------------

however the fortranwiki show that the intel fortran support the
fortran 2003 feature in Interoperability with C very well.
http://fortranwiki.org/fortran/show/Fortran+2003+status
From: Ian Harvey on
On 17/06/2010 6:08 PM, guoguo wrote:
> A module variable can not interoperate with a C global variable when I
> use intel fortran 11.1.065 or 11.1.053
> the source code:
> C language:
> -----------------------------------------------------------------------------------------------
> #include<stdio.h>
>
> int entier_externe;
> double double_externe;
>
> extern "C" void sp( float m1[6][3], double m2[][5], int *n, char *ch )
^^^^^^^^^^

This isn't C code, it is C++.

Try with:

extern "C" int entier_externe;
extern "C" double double_externe;
From: guoguo on
thank you very well
I try it , and the program runs very well

Ian Harvey 写道:
> On 17/06/2010 6:08 PM, guoguo wrote:
> > A module variable can not interoperate with a C global variable when I
> > use intel fortran 11.1.065 or 11.1.053
> > the source code:
> > C language:
> > -----------------------------------------------------------------------------------------------
> > #include<stdio.h>
> >
> > int entier_externe;
> > double double_externe;
> >
> > extern "C" void sp( float m1[6][3], double m2[][5], int *n, char *ch )
> ^^^^^^^^^^
>
> This isn't C code, it is C++.
>
> Try with:
>
> extern "C" int entier_externe;
> extern "C" double double_externe;