Solved

Fortran to C++ Conversion

Posted on 1997-03-17
1
607 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

806 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