Fortran to C++ Conversion

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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.