Accessing Mixed-Language, External Variables in FORTRAN

Last reviewed: July 17, 1995
Article ID: Q60076
The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 4.1, 5.0 and 5.1
  • Microsoft FORTRAN for OS/2, versions 4.1, 5.0, and 5.1

SUMMARY

The following is a sample C and FORTRAN mixed-language program demonstrating how to access external variables from within Microsoft FORTRAN.

FORTRAN Code

      SUBROUTINE ASSIGN
      INTEGER*2 X [EXTERN, ALIAS:'_extrn']

      X = 2
      END

C Code

#include <stdio.h>

extern void fortran assign(void);
int extrn;

main() {
    extrn = 5;
    printf("Before call to FORTRAN:  %d\n",extrn);
    assign();
    printf(" After call to FORTRAN:  %d\n",extrn);
}

MORE INFORMATION

To use global variables exported from another language in FORTRAN, the EXTERN attribute must be used. EXTERN tells the FORTRAN compiler that the variable is not local to the FORTRAN subroutine or function. Variables declared EXTERN will instead be resolved by the linker. You can use the ALIAS attribute to assign a different name to the imported variable; in this example, it is required because the underscore prepended by the C compiler is invalid in the FORTRAN naming convention.

No modifications should be necessary for the exporting module to allow FORTRAN to import the global variable(s).


Additional reference words: kbinf 4.10 5.00 5.10
KBCategory: kbprg kbinterop kbcode
KBSubcategory: FORTLngIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.