- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Iremark a strange behavior using COMMON in a C++ :
C++ Code :
extern "C" struct {int c, e, f;} com;
Fortran Code :
COMMON/COM/ C, E
integer(C_INT):: C
integer(C_INT):: E
bind(C, NAME="com"):: /COM/
When I compile and execute this, I've got no error even if the structure is not the same. Is there any reason ? Can I give a special option to ifort (or g++ for link) to raise an error if I have this kind of difference ?
Link Copied
- 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
Thanks for your response.
Maybe my question was not very clear. As you see the structure defined in C++ and the one defined in Fortran are not the same. I understand that it will not always working. My question was to know if there is any option to forbid this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You are only specifing a common name "com". You would have the same problem in C++ if each source used a different deffinition for the structure declared with the name of "com".
You might consider having both the C++ and F90 program use the same #include file (will require FPP) where the include file conditionally expandson languagebut is easire to maintain.
#ifdef _CPP
// com.inc
#pragma pack(1)
extern "C" struct {int c, e, f;} com;
#else
C COM.INC
COMMON/COM/ C, E, F
integer(C_INT):: C
integer(C_INT):: E
integer(C_INT):: F
bind(C, NAME="com"):: /COM/
#endif
This way the structure declarations are in one file.
I would also using modules and user defined types
#ifdef _CPP
// com.inc
#pragma pack(1)
struct MyCom {int c, e, f;};
#else
C COM.INC
type MyCom
sequence
integer(C_INT) :: C
integer(C_INT) :: E
integer(C_INT) :: F
end type MyCom
#endif
Your C++ app
#include "com.inc"
extern "C" struct MyCom com;
Your F90 module
module YourModule
#include "com.inc"
type(MyCom) :: COM
bind(C, NAME="com") :: COM
...
end module YourModule
Your F90 application
program YourProgram
use YourModule
...
COM%C = COM%E + COM%F
Jim Dempsey
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for your answer.
So if I understand well, I have to check everytime if my structure declared in C++ is the same as in Fortran. There's no way to check this automaticly during compiling or linking.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Let me suggest the following modification to the Fortran type:
type, bind(C) :: MyComThis creates an "interoperable" type which is guaranteed by the language to be the same as what the "companion C processor" would use.
integer(C_INT) :: C
integer(C_INT) :: E
integer(C_INT) :: F
end type MyCom
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for this tips.

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