After writing the enumeration you can compile your Pyrex
module.
There are two ways to build your module:
using gcc or
using
distutils.
Both methods have be verified to work on Linux, however
Windows™ users may find it easier to use
distutils.
To build your module using GCC is a three step process on Unix. (I have know idea how many steps it is on Windows™.)
compilerover your code as follows, where
mymodule.pyx
is the name of the
Pyrex module you are writing.
python2.2 pyrexc mymodule.pyx
Next, compile the resulting C file into a .o
file.
The call to gcc
looks like this:
gcc -c -fPIC -I/usr/include/python2.2/ mymodule.c
The arguments to gcc
are explained below.
.o
file instead of an executable.position independent code, so we can dynamically link against it later.
/usr/include/python2.2/
..o
into a .so
:
gcc -shared mymodule.o -lxosd -o mymodule.so
gcc
to put the output into a file
called mymodule.so
mymodule
.distutils
Instead of using the command-line (or make
) to
compile your modules, you may find it easier (especially on
Windows™) to use distutils
.
The following Python program, based on code provided by
Matthias Baas, uses distutils
to build
mymodule.pyx
.
from distutils.core import setup, Extension import os # Create the C file os.system("python2.2 pyrexc mymodule.pyx") # Make the extension module ("mymodule") link against xosd xosdExtn = Extension("mymodule", ["mymodule.c"], libraries=["xosd"]) # Compile the extension module setup(name="mymodule", ext_modules=[xosdExtn])
Save the above code in a file called setup.py
and run the following code to build and install your
module.
python2.2 setup.py build python2.2 setup.py install
After building your module, you can import the module into
Python 2.2 using the import
statement.
import mymodule print dir(mymodule)
Previous | Contents | Next |