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.

X
O
S
D

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.

    -c
    Produces a .o file instead of an executable.
    -fPIC
    Produces position independent code, so we can dynamically link against it later.
    -I/usr/include/python2.2/
    is the location of the Python 2.2 include file. The location of your Python include file may differ from /usr/include/python2.2/.
    mymodule.c
    is the name of the C file produced by Pyrex.
  3. Finally, link the .o into a .so:
    gcc -shared mymodule.o -lxosd -o mymodule.so
    -shared
    produces a shared-object file, instead of an executable.
    mymodule.o
    is the name of the module you wish to compile.
    -lxosd
    links against a C-library, with the name of the library given as the argument.
    -o mymodule.so
    causes gcc to put the output into a file called mymodule.so
    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 setup.py and run the following code to build and install your module.

python2.2 setup.py build
python2.2 setup.py 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