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!

mixed language programming issue

Status
Not open for further replies.

shredzone

Programmer
Jan 30, 2007
4
0
0
CA
Hi!
First I am new to Fortran.
I have a situation of C++ class calling fortran function
which needs to call back a member function of that class.
Below is the code that takes an "ugly" approach. Please let
me if there are other better solutions. If not, is it
okay to do something like what I have done. The code compiles and runs fine on Solaris and Linux.

// First the "C" code:
#include <iostream>

class A
{
public:
A(){}
void print () { std::cout << "HelloWorld\n"; }
};

extern "C"
{
void dumpit_( unsigned long* aval )
{
A* a= reinterpret_cast<A*>(*aval);
a -> print();
}
}

extern "C"
{
void iamfortran_( unsigned long* );
}

int main()
{
A a;
unsigned long a_addr = reinterpret_cast<unsigned long>(&a);
iamfortran_( &a_addr );
}

! Next the F95 code:
subroutine iamfortran( l )
integer(8), intent(in) :: l
interface
subroutine dumpit( i )
integer(8), intent(in) :: i
end subroutine dumpit
end interface

call dumpit( l )
end subroutine iamfortran
 
I don't see the purpose of that, what you do:
In C++ you have the function dumpit(), then in your main() you call the Fortran function iamfortran(), which calls then again the C++ function dumpit().

It is more convenient from your C main() to call first the Fortran function, which computes something (which you cannot do with C) and returns the results back to C, and after that from your C main() to call the C function dumpit(), which dumps the results computed by a Fortran subroutine.

Your fortran subroutine should not call the dumpit() function from C, because:
1. There is no purpose for that
2. This is bad program design, it causes spaghetti code which is hard to maintain.




 
This is an example code fragment. In reality I will have a psuedo RNG sitting in C++ and a driver sitting in the C++. The analytics in F95 needs to access the random generator (not random numbers - otherwise is mute - as you mention). I can wrap the random generator similarly to dumpit for example. The F95 analytics will use the generator and go through a fair bit of nasty calculation before returning the callee some results. (I really need to avoid creating a whole new library of RNG in F95 as well)

None of the above is shown in the code. The above code is meant to provide a trick and see if others in mixed programming language use a better or similar trick. If there are better "legal" options of doing the above let me know. In very simple terms is there a way to create a full cycle C++->F95->C++ (where F95 needs to call a member function of
a class).

I am suspecting this cant be done. But I am new to F95 and mixed language programming having spent all last 20+ years in C++ so I though no harm in asking the fellow programmers.

Thanks!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top