Extension module import error with MinGW, SWIG, and distutils

Mike C. Fletcher mcfletch at rogers.com
Fri Aug 9 18:13:04 CEST 2002


The key requirement seems to be using a tool to create import libs for 
the MingW compiler.  I've successfully managed to build (most) of the 
eGenix extensions (still working on TextTools).  The program to create 
the exports is called pexports.exe.

Here's a script that creates the .a files using pexports:

dlls = [
	"c:\\winnt\\system32\\python22.dll",
]


import os, sys
temp = "c:\\temp"

def toLib( dllfile ):
	"""Create lib file for a given dll"""
	directory, file = os.path.split( dllfile )
	base, ext = os.path.splitext( file )
	if not ext:
		ext = '.dll'
	deffile = os.path.join( temp, base+'.def' )
	libfile = os.path.join( sys.exec_prefix, 'libs', 'lib'+base+'.a' )

	command = "pexports %(dllfile)s > %(deffile)s"%locals()
	print command
	os.system( command )
	command = "dlltool -d %(deffile)s -l %(libfile)s"%locals()
	print command
	os.system( command )

for dll in dlls:
	toLib( dll )


Once you have that done for each DLL the extension relies on, MingW 
should (it seems) be able to build with the libs (but I haven't 
confirmed that it works, as I'm still in the middle of tracking down the 
problems with compiling mxTextTools).  I'm guessing there's another dll 
requiring wrapping (or a problem with Unicode, I suppose).

HTH,
Mike

Phil Schmidt wrote:
> Here's where I'm at with this so far:
> 
> I dumped SWIG for the moment to simplify things, and am using a simple
> example (spammodule) from the distutils documentation. Everything
> builds and installs ok, but I still get the import error described in
> my previous post.
...
> If anyone has any ideas about what the problem might be, please
> enlighten me!
> 
> pschmidt at omnimn.com (Phil Schmidt) wrote in message news:<69413f9.0208020512.56014eb6 at posting.google.com>...
> 
>>I've been slaving away (ok, not quite, but my head is about ready to
>>burst!) trying to create an extension module on a Win2K machine, using
>>MinGW, SWIG, and distutils. I've been working with a very simple
>>example, just for the sake of learning the ropes, and I am now to the
>>point where, when I attempt to import my extension module, I get the
>>following traceback:
>>
>>
>>>>>import MyDemo
>>>>
>>Traceback (most recent call last):
>>  File "<pyshell#0>", line 1, in ?
>>    import MyDemo
>>ImportError: DLL load failed: The specified module could not be found.
>>
>>
>>Here's a summary of the steps I followed to create the MyDemo module:
>>
>>1) I manually ran SWIG to create the wrapper file (because if I let
>>distutils call SWIG, it overwrites my C source file (and yes, I saw
>>the posts about that, so I need to update my distutils...))
>>
>>2) I then run distutils, which, except for a couple mild warnings
>>about some SWIG functions being defined but not used, runs without
>>errors and creates a MyDemo.pyd file.
>>
>>3) I copy the .pyd file to my Python22/DLLs directory, then try to use
>>it and get the error described above.
>>
>>4) I ran the depends.exe utility on my .pyd file, and it says that the
>>"WINNTSYSTEM32PYTHON22.DLL" file is missing. The full path reported
>>for this file is
>>"c:\gnu_demo\simpledemo\build\lib.win32-2.2\WINNTSYSTEM32PYTHON22.DLL".
>>Of course, this is incorrect, but I don't know how to fix it.
>>
>>5) As a test, I copied/renamed python22.dll to the path reported in
>>step (4), and tried again, with the same DLL load error.
>>
>>So, now I'm stumped (and frustrated)! It appears from the posts here
>>that it is possible to make this work, but somehow I'm missing
>>something.
>>
>>I would be grateful if anyone could offer any suggestions!
>>
>>Here are the files I'm working with right now:
>>
>>## demo.c ##
>>---------------------------------
>>int MyDemo(int x)
>>	{
>>	return x*(1-x);
>>	}
>>
>>
>>
>>## demo.h ##
>>---------------------------------
>>int MyDemo(int x);
>>
>>
>>
>>## demo.i ##
>>---------------------------------
>>/* File : example.i */
>>%module MyDemo
>>
>>%{
>>#include "demo.h"
>>%}
>>
>>int MyDemo(int x);
>>
>>
>>## setup.py ##
>>---------------------------------
>>from distutils.core import setup, Extension
>>
>>setup (name = "MyDemo",
>>       version = "0.1",
>>       description = "A Test!",
>>       author = "Phil Schmidt",
>>       author_email = "pschmidt at omnimn.com",
>>       ext_modules = [Extension("MyDemo", ["demo.c", "demo_wrap.c"])]
>>      )
> 


-- 
_______________________________________
   Mike C. Fletcher
   Designer, VR Plumber, Coder
   http://members.rogers.com/mcfletch/






More information about the Python-list mailing list