Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call to C++ from Fortran

Status
Not open for further replies.

sanyibacsi

Programmer
Mar 10, 2009
3
AU
Hi There,

I am new to this forum as well as to Fortran. I code in C++ for over ten years and now I received an assignment what requires me to provide an interface to a Fortran code to pass struct data back and forth.

The background is that both applications are mathematic ones and the Fortran should utilize some of the computations done in the C++ code as here (not very usual as far as I understand) the Fortran code is the alive and maintained code while the C++ code is the closed and no longer update one.

The C++ code is single threaded and normally instantiated and use somehow like this:
Code:
struct StructElements {
    int a, b, c, d;
    real a1, b1, c1, d1;
};
vector<StructElements> vecStructElements;
...
CCppComp Application("/var/etc/logxy.txt);
Application.setup(vecStructElements);
Application.solve("<derivation, integration, laplace, whatever>");

where

Code:
void CCppComp::Application.setup(vector<StructElements> &data);
void CCppComp::solve(const string Equ);

It looks that I can pretty much manage the setup and the solve change, at least based on the information I read so far. My problem is that I have no idea how to instantiate the class in Fortran and call the methods of it; or make it in a way that it works when put into the C++ code.

Do you know what would be the right way to do that?

Thanks :)
 
Problem is Fortran cannot handle vectors or any std collection. You need to set up an array which is used to populate the vector before passing it into the routine. On exit, copy the contents of the vector back to the array.

Const string isn't good practice - it creates a new copy of the string every time. Better to use const string&. String, like vector is a std namespace type. There is no standard translation in Fortran. Also, Fortran strings end with trailing spaces, not with a NUL character. The easiest way around this is to write a routine that takes a character array of a known size, searches backwards for the last non space character, stuffs it into a string and passes it to the routine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top