Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

C function call from fortran

cvnravi
Beginner
449 Views
Hi,

I have a funtion in 'C'. I want to call this funtion from fortran. this function contains 2 structures paseed by pointer. How do i do this in fortran.


Ex:
struct a
{
int x;
float y;
};
struct b
{
int p;
float q;
};

In c:
fun(*a,*b);

How do I call this from fortran. Here a is the input structure and b is output structure.
i am able to fill the structures in fortran but it is unable to hold the data.

Ho do i call fun(*a,*b) in fortran?

Please explain me..
0 Kudos
1 Reply
Yuan_C_Intel
Employee
449 Views

Hi,

Please refer to our compiler document on section: "intel Fortran/C Mixed-Language Program" for details.

For your problem specifically, there's one example on how to interoperate user defined structure from the compiler user guide.

Interoperability of Derived Types
For a derived type to be interoperable with C, you must specify the BIND(C) attribute:

TYPE, BIND(C) :: MYTYPE

Additionally, as shown in the examples that follow, each component must have an interoperable type and interoperable type parameters, must not be a pointer, and must not be allocatable. This allows Fortran and C types to correspond.

typedef struct {
int m, n;
float r;
} myctype

The above is interoperable with the following:

USE, INTRINSIC :: ISO_C_BINDING
TYPE, BIND(C) :: MYFTYPE
INTEGER(C_INT) :: I, J
REAL(C_FLOAT) :: S
END TYPE MYFTYPE

Example on Interoperability of Procedures:

Consider the following call to this C function:

intc_func(int x, int *y);

As shown here, the interface for the Fortran call to c_func must have x passed with the VALUE attribute, but y should not have the VALUE attribute, since it is received as a pointer:

INTERFACE
INTEGER (C_INT) FUNCTION C_FUNC(X, Y) BIND(C)
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
INTEGER (C_INT), VALUE :: X
INTEGER (C_INT) :: Y
END FUNCTION C_FUNC
END INTERFACE

0 Kudos
Reply