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

Accessing global variables defined in C

Karanta__Antti
New Contributor I
697 Views

From the documentation https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/global-data.html I can see how to make global variables defined in Fortran accessible from C. 

Is this possible the other way around? I.e. accessing a global variable defined in C from Fortran?

 

0 Kudos
1 Solution
3 Replies
Karanta__Antti
New Contributor I
620 Views

Thank you tcu for the link to the solution and Steve for a thorough explanation!

I was expecting the Fortran declaration to look different depending on whether accessing a variable defined elsewhere or defining a variable that can then be accessed elsewhere (c.f. extern in C). So exactly the same point as for the person who had posted the linked question.

0 Kudos
Steve_Lionel
Honored Contributor III
658 Views

That post is correct - in Fortran you declare a module variable (must be at module level and not in a procedure), and give it the BIND(C) attribute, specifying the case-sensitive name if not in all lowercase.) The standard says:

A C variable whose name has external linkage may interoperate with a common block or with a variable declared in the scope of a module. The common block or variable shall be specified to have the BIND attribute.

At most one variable that is associated with a particular C variable whose name has external linkage is permitted to be declared within all the Fortran program units of a program. A variable shall not be initially defined by more than one processor.

Reply