Compiling Your Module

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.


Building Your Module Using GCC

To build your module using GCC is a three step process on Unix. (I have know idea how many steps it is on Windows™.)

  1. Start by running the Pyrex compiler over your code as follows, where mymodule.pyx is the name of the Pyrex module you are writing.
    python2.2 pyrexc mymodule.pyx
  2. 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.

    Produces a .o file instead of an executable.
    Produces position independent code, so we can dynamically link against it later.
    is the location of the Python 2.2 include file. The location of your Python include file may differ from /usr/include/python2.2/.
    is the name of the C file produced by Pyrex.
  3. Finally, link the .o into a .so:
    gcc -shared mymodule.o -lxosd -o
    produces a shared-object file, instead of an executable.
    is the name of the module you wish to compile.
    links against a C-library, with the name of the library given as the argument.
    causes gcc to put the output into a file called
    The C library that you are wrapping will probably differ from mymodule.

Building Your Module Using 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 and run the following code to build and install your module.

python2.2 build
python2.2 install

Using Your Module

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

Michael JasonSmith
Last modified: Wed Jul 10 14:24:20 NZST 2002