- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The fact that the relevant areas are not populated probably gives rise to the exception I reported in an earlier thread.
My question is: Can C++ code containg classes be called successfully from Fortran? Are there any extra things to be aware of - compared to my dummy snipped example from my earlier thread?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My advice is to use access routines (get, put, etc.) if you need to communicate class data between the languages.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My advice is to use access routines (get, put, etc.) if you need to communicate class data between the languages.
I'm not trying to access C++ class info directly from Fortran, I am just calling a C++ function I wrote which instantiates, and then populates, an object. It is a small dummy Fortran main program.
extern "C" {
int cdecl Run_Great() {
// Create a GREAT model
Great_Model model;
// Set standard conditions
double s_temp = 15.5556; s_temp += 273.15; // C -> K
double s_press = 14.7; s_press *= 6.895*1.0e3; // psia -> Pa
model.setStandardConditions(s_temp, s_press);
// Create a layer and set appropriate properties
Layer_Model& layer1 = model.createLayer();
double kx = 45.0; kx *= 9.869*1.0e-16; // mD -> m2
double ky = 45.0; ky *= 9.869*1.0e-16; // mD -> m2
double kz = 45.0; kz *= 9.869*1.0e-16; // mD -> m2
layer1.setPermeability(kx, ky, kz);
If I step into this code from the Fortran driver, it successfully instantantiates "model", and populates s_temp, s_pres, etc, however after it creates "layer1", which is a class member of "model", it returns just fine, but layer1 has not been created or populated when I look at "model" in the Watch window. If I run from a dummy C++ main program, it all gets populated properly and I can see this in the Watch window.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Could well be. Unfortunately the C++ variable in question is a map and I've no idea how to look at it / access it. I think the main issues are that there are C++ complex data structures which may be causing problems when the main program is in Fortran (although none of these C++ data structures are visible to the Fortran caller, so that puzzles me).
I'm going to try to get the C++ guys to write C wrappers for all the functions in the C++ function Run_Great, then I can call them each individually from Fortran rather than by calling in theC+ function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
// Create a layer and set appropriate properties
Layer_Model& layer1 = model.createLayer();
double kx = 45.0; kx *= 9.869*1.0e-16; // mD -> m2
double ky = 45.0; ky *= 9.869*1.0e-16; // mD -> m2
double kz = 45.0; kz *= 9.869*1.0e-16; // mD -> m2
layer1.setPermeability(kx, ky, kz);
If I step into this code from the Fortran driver, it successfully instantantiates "model", and populates s_temp, s_pres, etc, however after it creates "layer1", which is a class member of "model", it returns just fine, but layer1 has not been created or populated when I look at "model" in the Watch window. If I run from a dummy C++ main program, it all gets populated properly and I can see this in the Watch window.
There's not a chance that layer1 is a dangling reference (the thing that it is referencing has ceased to exist)? It matches the symptoms you describe.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There's not a chance that layer1 is a dangling reference (the thing that it is referencing has ceased to exist)? It matches the symptoms you describe.
I'm no C++ expert, but it's my understanding that "layer1" is a pointer to the real layer inside "model". The model.CreateLayer() call creates one layer inside "model", and returns a pointer to it. This all works fine if the calling program is C, but not when it is Fortran.
It's not clear to me why I'm having this behavior when calling from Fortran, and I really don't know if creating C wrappers for each individual function call will help either. After all "Run_Great" is a C wrapper, and the code fails inside this when called from Fortran.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve,
Please can you tell me what you mean when you say;
"My advice is to use access routines (get, put, etc.) if you need to communicate class data between the languages."
What you mean by access routines exactly?
I have a similar function- the data I want my FORTRAN routine to get is a part of class data.
I need something like this;
MYFORTRANRoutine
call MYCPPRoutine (a,b)
That is in MYFORTRANRoutine I makea call to MYCPPRoutine from where I wan the values of a and b to be updated.
Since a and b are updated from the variables which are a part of the class in C++ code, I am ahving difficulty accessing it.
What exactly you mean by access routines here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I would like to ask (especially Steve) who has lot of experience of experience in mixed language programming) if this is possible:
1) I have a C++ class (called MyClass) and there is a private member function called 'Show'
2) 'Show' updates the member variables a,b,c,d which are private emmber variables of the class MyClass
3) What I do is: I define a public function which is an access Get function
4) Let it be called GetPrivMemberVariables
5) From GetPrivMemberVariables I call Show which consequently updated a,b,c,d
6) Now, as I said GetPrivMemberVariables is a public function of class MyClass
7) Can I call GetPrivMemberVariables from a free function which is not a member of the class MyClass?
Sandy

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page