probs w/ building python extensions on free Microsoft.NET SDK

Shakeeb Alireza sa at bayt.net
Mon Aug 6 23:15:24 EDT 2001


Failed Attempt at Building a python extension using free Microsoft.NET
SDK
(Python 2.1.1/win2k)

I'm a non-cs newbie whose first computer language is python... I'm
very happy with python, in fact I am not ashamed to say that I love
this language... But I'm also curious about other languages and now I
want to play around with some c extensions which I'd like to use in
python.
However, I'm totally frustrated by the experience of creating
extensions: nothing it seems is more unfriendly to newbies in the
python world (at least in my experience) than information on embedding
and extending python through c.

I've tried unsuccessfully in the past to compile python extensions
using cygwin, but failed for more than one reason or the other and
then just gave up. Recently I downloaded Microsoft's freely available
.NET SDK which contains the standard MS c/c++ compiler and libraries
and I thought this would be an opportunity to try the python extension
thing again.

(incidentally the SDK was downloaded from
http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-files/027/000/976/msdncompositedoc.xml&frame=true
)

Again, my main source of confusion was due to the documentation with
its intermezzos, dearth of clear examples, and its essential
assumption that the reader is already a proficient c coder and not a
newbie (graduating to c extensions). It further assumes you are using
the gui of visual studio and not the compiler directly via command
line.

Here's a short log of my experiences with a few of the pages in the
latest development docs and subsequent attempts to get it to work:

(Please note that my knowledge of handling the compilation of c code
is limited to tiny 'hello-world'-like programs.)

Problematic documentation:
--------------------------

Python Documentation: Extending and Embedding the Python Interpreter
    
4.1 A Cookbook Approach
    http://python.sourceforge.net/devel-docs/ext/win-cookbook.html
        
Is a highly confusing page for following reasons:
        
1. pyconfig.h is NOT included in PC dir of python 2.1.1 src
distribution in fact a file called config.h is included which is
exactly similar to the file that is already included in the include
directory of the win32 installer version (phew!!)
        
2. One is asked to "create a Setup file for your extension module"
except that the page referred to
[http://python.sourceforge.net/devel-docs/ext/building-on-unix.html#building-on-unix]is
actually for building extensions on UNIX. (now I'm totally confused)
        
3. The advice to "get David Ascher's compile.py script from
http://starship.python.net/crew/da/compile" is not useful because
David Ascher's pages are currently down.
        
4. Now one is supposed to open the DSW in visual c++ (alas I only have
the command line tools)... This becomes one of the those few moments
in the python world where a UI example triumphs over the command line
example to the point of displacing the latter (until later, that is)
        
            I think it is time to ABORT this effort!!! 
    
One more go there seems to be another page in the documentation and it
feels warmer.... DLLs have something to do with windows don't they?
    
4.3 Using DLLs in Practice 
    http://python.sourceforge.net/devel-docs/ext/win-dlls.html
        
- this actually has a command line example (great!!!) now to test....
 cl.exe is the standard windows c/c++ compiler use /LD for building
.DLLS
 add "F:\python21\include" to the INCLUDE environment variable
                
I'm going to use example.c from the src distribution 
(path: Python-2.1.1\PC\example_nt\example.c)

Problematic Compilation:
------------------------

########################################################################
ATTEMPT #1
########################################################################

D:\>cl /LD example.c F:\python21\libs\python21.lib

Microsoft (R) 32-bit C/C++ Standard Compiler Version 13.00.9254 for
80x86
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

example.c
f:\Python21\include\config.h(290) : fatal error C1083: Cannot open
include file:
 'basetsd.h': No such file or directory

D:\>
########################################################################
Action: deleted offending 'basetsd.h' from config.h
Microsoft probably didn't include it in this version of their SDK
########################################################################

########################################################################
ATTEMPT #2
########################################################################

D:\>cl /LD example.c F:\python21\libs\python21.lib

Microsoft (R) 32-bit C/C++ Standard Compiler Version 13.00.9254 for
80x86
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

example.c
f:\Python21\include\config.h(290) : warning C4142: benign redefinition
of type
c:\Program Files\Microsoft Visual Studio.NET\Vc7\include\stdarg.h(52)
: warning
C4142: benign redefinition of type
Microsoft (R) Incremental Linker Version 7.00.9254
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:example.dll
/dll
/implib:example.lib
example.obj
F:\python21\libs\python21.lib

########################################################################
(-: Great!!! example.dll sits happily in my folder... Now to test it: 
########################################################################

Problematic Execution:
-----------------------

D:\>python
ActivePython 2.1.1, build 212 (ActiveState)
Python 2.1.1 (#20, Jul 26 2001, 11:38:51) [MSC 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license" for more information.
>>> import example
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: dynamic module does not define init function
(initexample)
>>>

########################################################################
)-: ARGH!!!!! checked the source for example.c, and guess what: it
actually does define an initexample function.
########################################################################

Enough close-but-no-cigar situations. Enough hanging references to
usefuls tools and hints (-; Has anyone actually done this without
having to shell out for Visual Studio/C++... Dammnit! Bill doesn't
need anymore!!

Thanks for any feedback,

Shakeeb



More information about the Python-list mailing list