[Distutils] Sharing code between C extensions

Anders J. Munch andersjm@dancontrol.dk
Mon Oct 21 07:31:01 2002


Hi all,

I have a set of C extensions that I developed while using my own
Borland compiler Win32 Python build, statically linking the whole
thing.  Recently I gave up on using that special build, too much
hassle to get third-party stuff (like wxWindows) to work.

So now I'm using distutils, and I must say it's been surprisingly
smooth sailing so far.  Good work guys!

I've hit a snag, though: My C extensions (are supposed to) share code
and data.  But distutils insists that each extension be self-contained
and include everything that it depends on.

So that means that some of my C extensions and library code now exists
in multiple copies, one copy per extension.  I don't mind the memory
used; but the duplicated static data and duplicated type objects are
causing problems.

I guess placing the shared stuff in a DLL is an option but not one I'm
too fond of, for various reasons.  (Or perhaps I should switch to
Linux where shared objects behave differently? Well, some other time,
perhaps ;-))

What I'd like to do is to have multiple C extensions in the same .pyd.
Like, instead of writing (simplified):
     ext_modules = [Extension('foo', source=['foo.cc']),
                    Extension('bar', source=['bar.cc'])]
if I could do:
   ext_modules = [Extension(['foo','bar'], source=['foo.cc','bar.cc'])]
and have both extensions in the same .pyd.

Hmm... come to think of it I guess I could fake it with
   ext_modules = [Extension('foo', source=['foo.cc', 'bar.cc'])]
and then calling initbar() from initfoo().  I'd like to find a cleaner
solution though.

regards,
Anders