HOWTO: Access FORTRAN COMMON Block Contents From C++
ID: Q51614
|
The information in this article applies to:
-
Microsoft FORTRAN for MS-DOS, versions 4.0, 4.01, 4.1, 5.0, 5.1
-
Microsoft FORTRAN for OS/2, versions 4.1, 5.0, 5.1
-
Microsoft FORTRAN PowerStation for MS-DOS, versions 1.0, 1.0a
-
Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51
-
Microsoft C/C++ for MS-DOS, version 7.0
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
"extern public" 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 "extern struct" 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
"_far" 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 <stdio.h>
struct comstruct
{
float A;
long B;
char C;
};
extern struct comstruct _fortran TEST;
void _fortran cfunc(void)
{
printf("In cfunc\n");
printf("a is %f, b is %ld, c is %c\n", 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 : kb16bitonly kbFortranPS kbLangFortran
Version : :1.0,1.0a,4.0,4.01,4.1,5.0,5.1; MS-DOS:7.0; WINDOWS:1.0,1.5,1.51
Platform : MS-DOS WINDOWS
Issue type : kbhowto