Solved

Combine Fortran program and C++ program into one

Posted on 1998-01-28
5
372 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2013: API code to put hidden database icon in taskbar? 8 69
mapAB Challlenge 35 124
What is MicroStrategy.NET? 2 58
WPF - Tooltips for ComboBox items 5 35
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 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