I am doing a mixed c/fortran project and has a problem for accessing fortran common block from c
uisng the intel fortran mixed language example as below.
There is a common block kwaves in fortran program, and I want to access the data x, y in c++ program, when I debug this project, in the c subroutine, I can see that the kwaves has the address, but it does not show as a struct and I can not access to its data. Anyone know why?
zhiling LI
Fortran part:
PROGRAM fmain
IMPLICIT NONE
INTERFACE
SUBROUTINE c_routine (int_arg, str_in, str_out) BIND(C)
USE,INTRINSIC :: ISO_C_BINDING ! Declares C kinds
INTEGER(C_INT), VALUE,INTENT(IN) :: int_arg
CHARACTER(KIND=C_CHAR),DIMENSION(*) :: str_in,str_out
END SUBROUTINE c_routine
END INTERFACE
real*8 x, y
!DEC$ ATTRIBUTES alias :'kwaves'::kwaves
common /kwaves/ x, y
CHARACTER(80) OUTPUT_TEXT
INTEGER IN_ARG, OUTPUT_LEN
CHARACTER(80) INPUT_TEXT
INPUT_TEXT = "Testing..."C ! C suffix adds a null terminator
IN_ARG = 123
x=1.0
CALL c_routine (in_arg, input_text, output_text)
OUTPUT_LEN = INDEX(OUTPUT_TEXT," ")
IF (OUTPUT_LEN == 0) OUTPUT_LEN = 80
WRITE (*,*) OUTPUT_TEXT(1:OUTPUT_LEN)
END
C part
#include
#pragma pack(2)
extern struct kwaves_type
{
double x, y;
}kwaves;
#pragma pack()
extern "C" void c_routine (
int int_arg,
char* input_text,
char* output_text//,
//struct kwaves_type *kwaves1
)
{
sprintf(output_text,"%s%i ",input_text,int_arg);
}
Consider using BIND(C) to specify the external name of the common block. This should also remove any need to go a #pragma pack'ing. Consider using C_DOUBLE as the kind of the reals that are in the common block.
Note that you can interop with normal module variables too. Common blocks are an anachronism.
链接已复制
Consider using BIND(C) to specify the external name of the common block. This should also remove any need to go a #pragma pack'ing. Consider using C_DOUBLE as the kind of the reals that are in the common block.
Note that you can interop with normal module variables too. Common blocks are an anachronism.
Best wishes,
Zhiling LI