0

I have application written in C++ that uses SWIG for python integration.

Now under linux/osx when i build swig wrapper it creates so file that is used from application like this.

Py_Initialize();
PyRun_SimpleString("import MoBridge");
PyRun_SimpleString("a = MoBridge.MoBridge()");
PyRun_SimpleString("a.CreateQuadMesh()");
Py_Finalize();

What this does is it imports wrapper MoBridge, then it calls trough wrapper C++ function CreateQuadMesh(). Wrapper roughly looks roughly like this

h file:

#include "MoEngine.h"

class MoBridge
{
public:
    MoBridge();
    ~MoBridge();
    void CreateQuadMesh();
};

cpp file:

#include "mobridge.h"

void MoBridge::CreateQuadMesh()
{
    MoEngine::CreateMesh();
}

The wrapper calls MoEngine static function and it in turn does what it does.

Now this works great under Linux/osx if I understood it correctly because the way so file is linked.

But under windows I had to create DLL and as far as I found DLL files are loaded differently so they live in different memory from the rest of the application and hence cannot see applications other static methods.

I know that I can use dllexport to expose methods from dll to the rest of the application. But in this case I'm looking on how to allow dll to access rest of the applications static functions in applications memory.

I would appreciate any point in the right direction.

listener
  • 1
  • 5
  • http://stackoverflow.com/questions/8654327/use-static-class-variable-function-across-dlls http://stackoverflow.com/questions/4911994/sharing-a-global-static-variable-between-a-process-and-dll I think these consider your question. Give it a read – Mercious Mar 26 '15 at 08:22

1 Answers1

0

If anyone gets stuck with this I have found solution that will resolve this in both linux, osx and windows.

using shared object *.so will of course work with linux/osx but luckily there is even easier solution to use with SWIG that is really not documented in SWIG but it's documented in python documentation (thank you python!)

For this to work you don't need to create dll or so file from your wrapper but after swig creates your *_wrap.cxx file you should include it in your project and before calling Py_Initialize() you import your module like this.

PyImport_AppendInittab("_MoBridge", PyInit__MoBridge);

Then you can use as previously mentioned:

Py_Initialize();
PyRun_SimpleString("import MoBridge");
PyRun_SimpleString("a = MoBridge.MoBridge()");
PyRun_SimpleString("a.CreateQuadMesh()");
Py_Finalize();

And basically since you have your *_wrap.cxx in your project and python is essentially living within your application since you initialised it you have exactly same behaviour like if you have used so in linux/osx except this work on all three platforms.

Cheers!

listener
  • 1
  • 5