Calling Fortran functions from C in MSVC++5?

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.

gunnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
fasterCommented:
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

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
gunnAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.