Solved

Combine Fortran program and C++ program into one

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
seriesUp challenge 7 155
Which Microsoft MSDN Package Is The One I need? 3 45
countHi challenge 25 122
Please explain "Promise Pattern" and how it's used 2 56
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

810 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