Link to home
Start Free TrialLog in
Avatar of cw43
cw43

asked on

Combine Fortran program and C++ program into one

I have created a Visual C++ application (SDI).  I also have a Fortran application (routines) needed to be combine with the Visual C++ application under Visual Studio environment.  Could any one tell me how?  Do I need to compile Fortran routines into libraries in advance? How (by using Visual Studio)?  How do I call those functions in C++?  Thanks.

Richard
ASKER CERTIFIED SOLUTION
Avatar of hasmet
hasmet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cw43
cw43

ASKER

In Fortran, function arguments are actually passed by reference.  So even when I call my Fortran subroutines in C++, which function takes variables in Fortran, should I pass pointers instead variables in my cpp file?  Also, how to make libraries of  Fortran subroutines in Visual Studio on Win95?  

Thanks.
you need a fortran compiler to create your library, can not use VC++
/*     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  "this is your fortran file, use fortran compiler to C  create library for this
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

as you see, you can specify value or reference
Avatar of cw43

ASKER

I have compiled my Fortran code into a static libraty by using Digital Visual Fortran compiler, and called it in my C++ code like: extern void __stdcall myfunction().  Of course, I have linked the library in my Visual C++ 5.0 project.  However, it does not work.  The compiler complained about the unsolved external function.  What happened?
some fortran compilers are not creating compatible libs (such as watcom)
but digital visual fortran does create compatible one.

two if your fortran code doesnot specifically says [VALUE]
 the default is by reference so
extern void __stdcall PYTHAGORAS (float *a, floa t * b, float *c);

but above is for .c files, so if you are doing cpp project (C++ BUT NOT C)
then
extern "C" { void __stdcall PYTHAGORAS (float *a, floa t * b, float *c);}
 to say this is  C in C++

SO ABOVE ARE THREE problems you may have, other than these
it should run smoothly