Solved

Fortran to C++ Conversion

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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