Embedding Python in C/C++: Part 1

Using two languages in one program is a useful skill, if you want to become a very good programmer. So, in this post, I will teach you how to embed python in C or C++.

Sometimes, Python, although having a variety of modules for different functions, may not meet your needs, either because the necessary functionality may not exist, or the speed of the python script is not enough. So, to enhance its capabilities, we intersperse it with C or C++. By doing this, we are extending the capabilities of Python.

Python provides a rich set of APIs that can be used to embed it other languages. These APIS also allow C or C++ programs to call Python scripts by embedding the Python interpreter in an existing C or C++ program.

To this end, we will use SWIG(Simplified Wrapper Interface Generator) which can be downloaded from www.swig.org. The latest version, at the time of this writing is 2.0.9. However, if you do not want to go through the process of downloading and installing(which can be a pain in the neck), there is a simpler way, at least for Ubuntu users. Go to the software centre of Ubuntu. In the search bar, type SWIG. There will only be one result, click on it and say install. It will be installed in your system.

Also, you will need the Python interpreter installed in your system. The Python package consists of he Python executable, Python interpreter and Python library. These files are located at the following paths respectively, /usr/bin/python, /usr/lib/python2.7, /usr/lib/python2.7/config.

Also, in the Python library, libpython2.7.so and makefile.pre will have to be present. Otherwise, you will have to download a development package.

To extend Python, we create a simple interface file, describing the functions we want to access from Python. SWIG then automatically generates the code required to interface Python with the specified functions. SWIG directives provide additional control over the wrapping process.

The process of making C functions available to Python is often referred to as “wrapping”.

The SWIG generated wrapper file is compiled and linked against the Python interpreter library, producing a shared extension module. The wrapper file could also be linked directly into a custom version of the Python interpreter, or linked into a host program that will be embedding the Python interpreter.

The following steps will be followed while embedding Python:

  1. Identify the functions, structures and objects which you wish to make available to Python.
  2. Create interface functions (often wrapper functions)  that can be called by the Python interpreter.
  3. Compile the interface functions(wrapper module)
  4. Link the wrapper module against the target function library and Python library.
  5. Test the module

These are the steps to be followed. That is all for now. In the next part, I will teach you the actual code to do the embedding.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s