Solved

Calling Fortran functions from C in MSVC++5?

Posted on 1997-12-07
2
337 Views
Last Modified: 2013-11-08
I was wondering what kind of settings and specifics are needed to setup in order to call Fortran functions from C code and vice versa. I have Visual Fortran from DEC and MSVC++5.0.

I have determined how to call C functions from Fortran code, by changing the Fortran settings to place an underscore at the end of the fortran function names, and to use all lower case. (Our C functions are all like 'void function_(...) )

The problem comes when I want to go the other way; calling Fortran functions from C. I noticed in some of the online-help that in __cdecl calling convention, the name of the C function stays the same, but when compiling the Fortran code, I get unresolved linker errors of _function_@12 could not be resolved.

This leads me to think I need to use the __stdcall calling convention, as this adds the @12 to the end of the functions, which is the same that the default Fortran compiler does. Is this the way I need to go? I'm just wondering then if I'll run into other problems using __stdcall....

I just would like to know the standard way of how people do the Fortran/C thing.

Thank you.

0
Comment
Question by:gunn
2 Comments
 
LVL 7

Accepted Solution

by:
faster earned 100 total points
ID: 1256542
This is from the help comes with VC4.2:

This section applies the principles in the section “Overview of Mixed-Language Issues” to a typical case involving one function call and one subroutine call from C to Fortran. Default conventions are assumed for Fortran, so adjustments are made to the C code.
The C main program uses the __stdcall keyword to call the Fortran routines with the correct calling convention. The C source must use all-uppercase names for the routines, because this Fortran code does not use the C, STDCALL, or ALIAS attributes. Finally, pass by value and pass by reference are specified explicitly, though pass by reference would have been assumed by default for Fortran.
 
/*     File CMAIN.C   */

#include <stdio.h>

extern int __stdcall FACT (int n);
extern void __stdcall PYTHAGORAS (float a, float b, float *c);

main()
{
    float c;
    printf("Factorial of 7 is: %d\n", FACT(7));
    PYTHAGORAS (30, 40, &c);
    printf("Hypotenuse if sides 30, 40 is: %f\n", c);
}

C    File FORSUBS.FOR
C
      INTEGER*4 FUNCTION Fact (n)
      INTEGER*4 n [VALUE]
      INTEGER*4 i, amt
      amt = 1
      DO i = 1, n
        amt = amt * i
      END DO
      Fact = amt
      END

      SUBROUTINE Pythagoras (a, b, c)
      REAL*4 a [VALUE]
      REAL*4 b [VALUE]
      REAL*4 c [REFERENCE]
      c = SQRT (a * a + b * b)
      END
 

 

0
 

Author Comment

by:gunn
ID: 1256543
These words in the response tipped me off: "because this Fortran code does not use the C, STDCALL, or ALIAS attributes". I went back and looked up the calling convention of this 'C' option and found it to be exactly like the default __cdecl of C functions.

So, I found the setting on the FORTRAN tab of the project settings to change the 'Default Calling' to "C by Reference". That did it!!

The program compiled and linked like a charm..thanks!

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 106
NotAlone Challenge 20 70
noX challenge 17 76
Problem to start Neon 20 51
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now