Accessing fortran common block varibles from C++

NOTE:  For starters I renamed main_as_program.f and main_as_subroutine.f to main_as_program.c and main_as_subroutine.c so I can upload files.  

My object is to  access the common block variables  zzm_mmm,  mmm and  filename from my C++ program (main.cpp).   To do this I compile main_as_subroutine.f and create a library file that I use to link with my C++ program.    At issue.  The variables  zzm_mmm,  mmm and  filename are not reflected in an objdump of the library file (see how_to_run.txt).   How do I link in and access the common block variables from C++?


NOTE:  main_as_program.f is shown for reference and reflects the fortran code in its entirety.  


test.cmn
       implicit none

       real*4 ranUnif
       external ranUnif

       real*4 ranGen
       external ranGen

       real*4 ranRal
       external ranRal




c special common block
       integer tx_input, ptype, lx, mw, mmm ,zzm_mmm   

       character*128 filename, filename1
       character*128 ord
       character*1 fula
       character*10 zzm_lext

       common/input_int/tx_input, ptype, lx, mw, mmm ,zzm_mmm   

Open in new window

program.zip
forums_mpAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Karrtik IyerSoftware ArchitectCommented:
Just a suggestion :
I found the below tutorial good in accessing common blocks across C and Fortran. Please check if it is useful for you.
http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html

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
sarabandeCommented:
create a c structure which matches exactly the common block. as far as i remember, fortran has a packed format. hence you need to switch to 1-byte alignment  for the c structure as well.

add a byte equivalence (integer*1) to your common block. then you can pass the byte array via a function call to c. fortran passes variables by reference, what is a pointer to unsigned int in c and c++. you also should pass the size of the byte buffer by value, what is an int argument in c. then you could use the given size as a check that both structures have same size. if ok, you may cast the given pointer to pointer of struct and store it as a member in some class. you even could pass it to a constructor of a class and use it to initialize a struct member of that class which was declared by reference:

 struct MyCommon;
class FortranCommonMgr
{
      MyCommon & common;
      ~FortranCommonMgr() { delete &common; }
 
public:
    FortranCommonMgr(MyCommon * pCommon) : common(*pCommon) {}
    static void InitFortranCommon(unsigned char * pEquiv, int sizBuf)
    {
          if (sizBuf == sizeof(MyCommon))
         {    
                 Globals::Store(new FortranCommonManager((MyCommon *)pEquiv));
         }
   }  
   ...
};

Open in new window


the above code would store the pointer to FortranCommonManager to a static member of class Globals. alternatively you could store the pointer to MyCommon directly somewhere. or you make the manager class a singleton and provide the pointer via a static member function.

Sara
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
C++

From novice to tech pro — start learning today.