Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link to existing boost python module

I have wondered about this on and off but I never really got a definite answer. Is it possible within the boost.python framework to link against another boost.python module.

For example I have exported class A within boost_python_module(libA) and function B(A a) within boost_python_module(libB). Is it possible to specify in libB to link to A of libA.

The other way of looking at this problem would be that right now I have to generate all my bindings in one shot within one module. Is it possible to generate bindings incrementally over several boost_python_module.

like image 523
Sandeep Avatar asked Nov 15 '22 09:11

Sandeep


1 Answers

The Boost.Python way to handle what you are asking for is to divide your package in compilation units as explained in the tutorial and later do a merge in a main compilation unit that actually declares the modules.

You cannot link independent modules in Boost.Python because they declare specific Python entry points that are executed by Python when you load your module. For example, if the binary module name is mod.so, the Python interpreter will look for a function called init_mod (that is what BOOST_PYTHON_MODULE(mod) declares) and execute the code of that function. Within the code of that function, it expects to find Python C-API declarations of objects (instances, classes, etc).

If you link, for example, the mod.so binary to another module binary (say, foo.so), when Python loads mod.so, it would only find and execute init_mod and will ignore init_foo.

like image 130
André Anjos Avatar answered Dec 24 '22 14:12

André Anjos