Solved

Calling Fortran functions from C in MSVC++5?

Posted on 1997-12-07
2
343 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 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

729 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