[cairo] successfully built bindings via SWIG

Kyle Johnson osmaker+cairo at gmail.com
Tue Mar 18 19:55:59 PDT 2008

I've been wanting to use Cairo with Python for awhile now, but the
Python bindings are a bit out of date.

However, I have successfully built working bindings with SWIG for
Python without having to code much at all (should work with Ruby,
Perl, etc as well). This is based off the trunk from last night.

Relevant code (will need some dir location changes depending on where
you have cairo installed; use pkg-config to find them)...

cairo.i (with preprocessor directives added to cairo's headers, this
file wouldn't be needed):

%module cairo
#include <cairo-features.h>
#include <cairo.h>
#include <cairo-deprecated.h>

%include "/usr/local/include/cairo/cairo-features.h"
%include "/usr/local/include/cairo/cairo.h"
%include "/usr/local/include/cairo/cairo-deprecated.h"

setup.py (run with python setup.py build_ext -i):

from distutils.core import setup, Extension

setup(ext_modules = [Extension(
    "_cairo", # name of output library
    ["cairo.i"], # swig file
    libraries = ["cairo"], # link with cairo lib
"/usr/local/include/libpng12", "/usr/local/include/pixman-1"]

Mac OS 10.5 users will need to run setup.py like this (unless you
specifically compiled cairo as a universal binary):
env ARCHFLAGS='' python setup.py build_ext -i

I hope this is helpful. It's probably a lot easier to maintain SWIG
bindings than individual bindings for Java, C#, Perl, PHP, Python,
Ruby, Lua, Scheme, and Ocaml.

Kyle Johnson

More information about the cairo mailing list