Solved

Combine Fortran program and C++ program into one

Posted on 1998-01-28
5
396 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
[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
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

623 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