Solved

Combine Fortran program and C++ program into one

Posted on 1998-01-28
5
369 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:cw43
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
hasmet earned 100 total points
ID: 1410542
in your header file
extern int __stdcall myfunction(int n, double x);


then you compile and make a library of your fortran subroutines,
and link to VC++ project. then
use myfunction() in your cpp file
0
 

Author Comment

by:cw43
ID: 1410543
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.
0
 
LVL 1

Expert Comment

by:hasmet
ID: 1410544
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
0
 

Author Comment

by:cw43
ID: 1410545
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?
0
 
LVL 1

Expert Comment

by:hasmet
ID: 1410546
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

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This video teaches viewers about errors in exception handling.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

759 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

24 Experts available now in Live!

Get 1:1 Help Now