tags:

views:

41

answers:

1

I'm trying to build a C++ extension for python using swig. I've followed the instructions below and the others to a T and can't seem to get my extension to load.

I ran across this article on the MinGW site under "How do I create Python extensions?"

http://www.mingw.org/wiki/FAQ

I also found these tutorials:

http://boodebr.org/main/python/build-windows-extensions http://www.mail-archive.com/[email protected]/msg04655.html http://oldwiki.mingw.org/index.php/Python%20extensions

I'm using Panda3d-1.7.0 to build against - panda on win32 is running python2.6.4 (MSC v.1500 compiled). I'm using MinGW gcc/g++ (GCC) 3.4.5 to compile.

I've noticed that when I run setup.py with the following command:

python setup.py build -cmingw32

gcc.exe runs first, then g++.exe to build the pyd. g++ is linking against: -lpython26 -lmsvcr90

she builds and links well enough (no errors) but, when I copy the _extension.pyd and extension.py files over into Panda3d-1.7.0\python\Lib\site-packages and run > python -c "import extension" from the command line, Python dumps the following:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Panda3D-1.7.0\python\Lib\site-packages\extension.py", line 25, in <module>
    _bullet = swig_import_helper()
  File "C:\Panda3D-1.7.0\python\Lib\site-packages\extension.py", line 21, in swig_import_helper
    _mod = imp.load_module('_extension', fp, pathname, description)
ImportError: DLL load failed: The specified module could not be found.

Any tips or pointers? Thanks!

ct

+1  A: 

Two things to verify:

  • Check the C runtime library DLL bound to your python and to your extension DLL with dependency walker to make sure that they are using the same CRT. This is a common source of trouble when building extensions for other languages. (I see it often with Lua, for instance) and can cause interesting and intermittent bugs that are particularly difficult to track down.

  • IIRC, the "official" Python releases for Windows have switched to Visual Studio Express from MinGW. This may make it nearly impossible to use MinGW to build a C++ extension that can be called from a Python compiled and linked with Visual Studio due to irreconcilable differences in the C++ ABI.

Both of these add up to advice to make sure that the ABI you are assuming in your extension DLL matches the ABI assumed by the hosting application, and that you aren't duplicating fundamental functional blocks such as the CRT that might contain or manage state that is difficult to correctly share between instances.

Edit: The improved edit of the question speaks strongly to there being issues with mismatches between Microsoft's C++ ABI and the GCC C++ ABI which are known to differ.

RBerteig
Thanks for the assist. The dependency walker is pointing out that some of the lib*.a files I've linked against are missing their respective dlls in my path I'm going to play with that for a bit, when it's resolved I'll get back with you.
ct
Just checked everything out and it looks like that was my problem - gcc/g++ were linking against a lib*.a that didn't have it's companion dll in my %PATH% - thanks again for the advice and the href to the dependency walker, saved my day!
ct
@ct, Note also that dependency walker can be scripted. I've included it as part of my normal test suite before building a release or installer to make sure I've packaged all the needed DLLs, for example. It is one handy tool. Glad to have helped.
RBerteig