Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help >  Languages

Python and C/C++

Contents

This page shows how to write C and C++ programs that can be called from python. Extending and Embedding the Python Interpreter has more information.

C

This uses an example from Extending Python with C or C++, providing more information on how to make it work on Linux but less information on why it works

You should see a list of files that are in the current folder.

C++

This example shows how to interface with some user-written C++ code. It doesn't try to emulate full C++ functionality from python.

C++ using SWIG

You may need to make available from Python more of the class-related behaviour of the C++ code than the previous example provided. swig can automatically generate the interface code. See Extending Python with C++ for details. Here a short example will be provided.

This C++ code

#include <iostream>
using namespace std;

class Storage {
public:
  Storage() { value=999; }
  Storage(int v) { value=v; }
  int value;
  int doublevalue() { return value*2; }
};

int main()
{
  Storage s1, s2(7);
  cout << "s1.value=" <<s1.value 
       <<" s2.value=" <<s2.value 
       <<" s2.doublevalue()=" << s2.doublevalue() << endl;
}

can be translated into python as

class Storage:
   def __init__(self,v = 999):
       self.value = v
   def doublevalue(self):
       return self.value*2

s1=Storage()
s2=Storage(7)

print  "s1.value=", s1.value, " s2.value=", s2.value, " s2.doublevalue()=", s2.doublevalue() 

but let's suppose that doublevalue is a complicated C++ function that we don't want to convert into Python. How can we interface the Python and C++ code? First create these 3 files

storing.h
// storing.h
class Storage {
public:
  Storage();
  Storage(int v);
  int value;
  int doublevalue();
};
storing.cc
// storing.cc
#include "storing.h"

Storage::Storage() { value=999; }
Storage::Storage(int v) { value=v; }
int Storage:: doublevalue() { return value*2; }
storing.i
%module storing

%{
#include "storing.h"
%}

%include "storing.h"

Then run

   swig -c++ -python storing.i

If this succeeds it will silently create storing_wrap.cxx (which is likely to be rather large). Then create this file

setup.py
from distutils.core import setup, Extension

storing_module = Extension('_storing', sources=['storing_wrap.cxx', 'storing.cc'])

setup(name='storing', version='0.1', 
      author='My Name', 
      description="""Storing SWIG Module.""", 
      ext_modules=[storing_module], py_modules=['storing'])

and run

     python setup.py build_ext --inplace

This will display some compile lines and may produce a warning message

storing_wrap.cxx:2796: warning: 'argv[0]' may be used uninitialized in this function

Don't worry. You should now be able to run this in python

from storing import *

s1=Storage()
s2=Storage(7)

print  "s1.value=", s1.value, " s2.value=", s2.value, " s2.doublevalue()=", s2.doublevalue() 

and get the output

      s1.value= 999  s2.value= 7  s2.doublevalue()= 14
© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: February 2011