Solved

Fortran to C++ Conversion

Posted on 1997-03-17
1
598 Views
Last Modified: 2013-11-08
I am trying to learn C++ with no significant experience in programming. I have a heap of Fortran code that I need to convert into C++ (or preferably use the .OBJ code or .DLL) to integrate into my own program (C++). What steps should I take to overcome this problem. Assistance would be greatly appreciated.

BACKGROUND INFO:

I AM USING BORLAND C++ VERSION 4.5;
FORTRAN HAS BEEN COMPILED USING LAHEY;
MY PROGRAMMING KNOWLEDGE IS LIMITED;
0
Comment
Question by:forward
1 Comment
 
LVL 1

Accepted Solution

by:
josv earned 100 total points
Comment Utility
Hi,

There is a public domain tool named 'f2c' which actually converts an existing Fortran program to C. This tools is available on UNIX (Linux, FreeBSD) but I reckon that there is a Windows version available as well. Most cleanly written (or generated) C code can be compiled by the C++ compiler as well.

The big problem with f2c is that it tends to obscure the original source. Once converted to C, the original program structure is as good as gone. This makes it difficult to adapt the code after the conversion. Furthermore, if you have used any non-standard Fortran extensions, f2c will probably break or require you to edit the generated C code after conversion.

Your best option is to use the compiled Fortran code (.OBJ) files and link them into your C++ program. It is also possible to call a Fortran function in a Fortran DLL from C++, but this is slightly trickier.

You have to add the Fortran .OBJ files to your C++ project. Another possibikity is combining the Fortran .OBJ's in a .LIB, and adding the .LIB to the project.

Suppose you have a Fortran routine called FSUB, which takes two parameters, a 32 bit signed integer and a double precision floating point value. In C++ you have to specify the prototype for the Fortran function:

extern void _stdcall FSUB(int,double);

You can then call the Fortran routine FSUB from C++ like any other routine. By the way, when using Windows 3.x, the _stdcall keyword has to be replcated with _pascal.

Above solution works from MS Fortran to MS C++. A possible problem could be that the Lahey Fortran compiler generates code with different function names. In this case you should consult the Lahey documentation in order to "solve" the mixed language problem. This documentation has to provide with information as how the functions are named and how the parameters are passed.

Jos Visser
josv@osp.nl


0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

762 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

10 Experts available now in Live!

Get 1:1 Help Now