Microsoft KB Archive/51614

= How To Access FORTRAN COMMON Block Contents From C++ =

Article ID: 51614

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft FORTRAN Compiler 4.0
 * Microsoft FORTRAN Compiler 4.01
 * Microsoft FORTRAN Compiler 4.1
 * Microsoft FORTRAN Compiler 5.0
 * Microsoft FORTRAN Compiler 5.1
 * Microsoft FORTRAN Compiler 4.1
 * Microsoft FORTRAN Compiler 5.0
 * Microsoft FORTRAN Compiler 5.1
 * Microsoft FORTRAN PowerStation 1.0 Standard Edition
 * Microsoft Fortran PowerStation 1.0a for MS-DOS
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.51
 * Microsoft C/C++ Professional Development System 7.0

-



This article was previously published under Q51614



SUMMARY
An application developed in Microsoft FORTRAN can store data in a COMMON block of memory that has the Microsoft C equivalent of a public structure. An application developed in C can access a FORTRAN COMMON block as an &quot;extern public&quot; structure. The only public symbols in FORTRAN are the names of COMMON blocks and of subprograms. Because FORTRAN does not have a syntax to declare an external COMMON block, FORTRAN code always creates the memory locations for data in a mixed-language programming environment.

The most critical aspect to accessing a COMMON block from C code is using the correct naming convention. In C, all public names must have a leading underscore (_) character; FORTRAN code must declare the underscore explicitly. C and FORTRAN each expect public names to contain only uppercase letters. To enforce the correct naming convention, either use the _fortran attribute on the &quot;extern struct&quot; declaration in the C code or use the [C] attribute on the COMMON block name in the FORTRAN code.



MORE INFORMATION
The following project demonstrates using a FORTRAN COMMON block in an application developed in C. The project uses the _fortran keyword to allow the C code to generate the correct name for the COMMON block. Even though the design of the makefile assumes Microsoft FORTRAN version 5.1 and Microsoft C/C++ version 7.0, the information is generally correct for other versions of these compilers. This project creates a driver program, a FORTRAN subprogram that defines a COMMON block, and a C program that accesses the data in the COMMON block. The driver program is written in FORTRAN, it could have as easily been written in C.

A critical switch used in the C compile line is /Gx-. If this switch is not used, you will not be able to access the common block, unless you use &quot;_far&quot; in the extern declaration, in CFUNC.C as follows: extern struct comstruct _fortran _far TEST;

Sample Code #1: FORMAIN.FOR
C Main driver program C Compiler options required: None

PROGRAM COMMONTEST CALL FORSUB END

Sample Code #2: FORSUB.FOR
C A FORTRAN subroutine with a COMMON block C Compiler options required: None

SUBROUTINE FORSUB COMMON /TEST/ A, B, C        REAL A         INTEGER B         CHARACTER*1 C

A = 1. B = 2 C = 'F'        CALL CFUNC PRINT* PRINT*, 'IN FORSUB FOLLOWING CALL TO CFUNC' PRINT*, 'A IS ', A, '          B IS ', B, '           C IS ', C         RETURN END

Sample Code #3: CFUNC.C
// A C function that uses data in the FORTRAN COMMON block

#include 

struct comstruct {     float A;      long  B;      char  C;   };

extern struct comstruct _fortran TEST;

void _fortran cfunc(void) {     printf(&quot;In cfunc\n&quot;); printf(&quot;a is %f, b is %ld, c is %c\n&quot;, TEST.A, TEST.B, TEST.C); TEST.A = TEST.A + 1.; TEST.B = TEST.B + 1; TEST.C = 'C'; }

Sample Code #4: MAKEFILE
all: test.exe

formain.obj: formain.for fl /c /AL /Od /Zi formain.for

forsub.obj: forsub.for fl /c /AL /Od /Zi forsub.for

cfunc.obj: cfunc.c     cl /c /AL /Od /Zi /Gx- cfunc.c

test.exe: formain.obj forsub.obj cfunc.obj link formain forsub cfunc, test, nul, /nod /noe /co \ llibc7 llibf7rc oldnames;

Additional query words: nofpsnt fail

Keywords: kbhowto kblangfortran kb16bitonly KB51614

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.