?
Solved

Calling Fortran functions from C in MSVC++5?

Posted on 1997-12-07
2
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
faster earned 300 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

770 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