Microsoft KB Archive/11021

From BetaArchive Wiki

Calling C Modules from FORTRAN

PSS ID Number: Q11021 Article last modified on 12-19-1988

3.x XENIX

Summary: Below is an application note concerning the calling of C modules out of FORTRAN. The example clarifies the use of this technique. To receive this application note, contact Microsoft Product Support Services at (206) 454-2030.

More Information: Calling a C module is the same as calling a C function. Create the following C program that includes the function that you want to use in C, (the computer will emit a tone): #include <math.h> #include <conio.h> long tone (pRANGE) long int pRANGE; int spk_port = 97; int t_io; int d_factor; int d_count; int range; range= (short int) pRANGE; for (t_io=inp(spk_port) & 0xFC,d_factor=range;d_factor>-1;d_factor–) outp (97,t_io); for (d_count=d_factor;d_count>0;d_count–); outp (97,t_io+2); for (t_io=inp(spk_port) & 0xFC,d_factor=0;d_factor<range;d_factor++) outp (97,t_io); for (d_count=d_factor;d_count>0;d_count–); outp (97,t_io+2); After compiling this program, which gives you the C-object module, you must create a FORTRAN program that calls the function in the C module that you want to use. To do this, consult the FORTRAN manual sections concerning “Mixed-Language Programming.” The following is the FORTRAN program: interface to integer4 function tone[[range%5Breference%5D|C]] integer4 range end program main integer4 tone integer4 range integer*4 k range=300 k=tone(range) end After you have compiled the FORTRAN example, link the two object modules together. The C program is called TONE.C and the FORTRAN program is called CALLTONE.FOR. After compiling the two programs, do the following: LINK TONE.OBJ CALLTONE.OBJ; It has been verified that this works correctly in DOS Version 3.10 (using DOS FORTRAN Version 3.31 and DOS C Version 3.01). This should also work correctly in XENIX. For more information on the INTERFACE procedure and calling procedures in C from FORTRAN, please see the “Microsoft FORTRAN Compiler User’s Guide,” Section 8.4, “Writing Interfaces to Pascal or C from FORTRAN,” and Section 8.5, “Calling Procedures in Pascal or C from FORTRAN.” An explanation on how to pass parameters by pointers rather than by reference, as was done in these programs, also appears in these sections, on the same pages in the DOS manual. The only change you need is to compile the C program using a large model. If you compile it in XENIX, specify that you only want the C-object code (“cc -c”). You can then compile it together with the FORTRAN (“cl calltone.f tone.o”).