Solved

Fortran to C++ Conversion

Posted on 1997-03-17
1
613 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
ID: 1162414
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
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.

839 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