Assembla home | Assembla project page
 

Statically Linked Extension Modules

Once you link the python standard lib into your exe, you won't be able to stop. You'll write a couple of modules and want to link those in, too. You'll want to include PyQt so you can write gui code for your app as well. I wrote the API for our scripting engine using sip to generate the code for the python module, and that code is statically linked as well.

This all seems pretty easy, but you need to add --disable-shared to your python configure flags, and you need to do some special things to get the interpreter to initialize the modules correctly.

First, explicitly declare the init functions and a 'struct _inittab' for the modules you statically linked:

extern "C"
{
  void initsip();
  void initmyappengine();
}

Then, the magic trick is to call the import function explicitly followed by with _PyImport_FixupExtension() to register it properly. This isn't documented anywhere, and I got the tip from some nice guy in #python-dev.

  // this is required for statically linked modules
  initsip();
  _PyImport_FixupExtension("sip", "sip");
  initmyappengine();
  _PyImport_FixupExtension("myappengine", "myappengine");

You may have read in the documentation about automatically adding your init functions with PyImport_ExtendInittab, but I haven't found this to be of any help when using static modules, and actually I have no idea what good it is to anyone.