building foo.pyd, _initfoo vs. initfoo

Daniel Franke franke.daniel at gmail.com
Sun Jan 22 19:45:13 EST 2012


Hi all.

Usually I work on Linux and all my cmake-built Python extensions working there 
without trouble. Now these things need to work on Windows as well. While the 
code itself compiles fine, linking and loading makes trouble.

First of, to successfully link everything with mingw (g++-4.4.0, somewhat 
oldish) I had to define:

	-DPy_ENABLE_SHARED -DPy_BUILD_CORE

Not sure if this makes sense, but if it works, fine by me. Now, loading the 
module is different. 

First attempts of loading the module failed miserably until I found out that 
Python expects files with a .pyd suffix. Very innovative to throw off the 
layman. Took that hurdle.

Finally, on load of foo.pyd, I get:

	"Import Error: dynamic module does not define init function (initfoo)"

As this module loads fine in Linux, there is an initfoo() defined. Checking 
with `nm`, I find two things:

 1. The function name is mangled (__Z12initfoov)
     -> the module is compiled with g++, but I believed that PyMODINIT_FUNC
        includes the 'extern "C"' apparatus?

 2. Manually enclosing the init function with 'extern "C" {}" still yields the
    same error
     -> the function name also comes with a leading underscore '_', i.e.
        "_initfoo", not "initfoo"

Could someone knowledge with Windows and MinGW help me to sort this out? Tools 
used: cmake-2.8.3, mingw-? with gcc-4.4 and Python-2.7.2 from python.org.

Thanks

	Daniel




More information about the Python-list mailing list