Running C++ code in Jupyter Notebook

This post walks readers through the process of installing Cling kernel, so that they can run C++ code in Jupyter Notebook. The post also comes with links to other worked code examples.


I find Jupyter Notebook (aka iPython Notebook) an amazing tool – you can put python code snippets that run interactively, markdowns, mathematical expressions, plots, etc. all in the same place and share with others. With cell magic, you can also run code written in a number of other languages interactively.

But how about C++? If your work involve some quick tryouts, writing only a few lines of code to interact with other libraries, or getting someone started in the language, it is tempting use an interactive environment and avoid the following:

  • Writing a huge makefile and wonder why it doesn’t work, or
  • Spending a few hours setting up your project on your favourite IDE before realising it gets everything wrong
  • Waiting another few hours for the libraries to link, then reaching a segmentation fault as you are passing a null pointer to a badly-written research library
  • Spending the rest of the evening consoling someone as they have a nervous breakdown learning the language

Unfortunately C++ is not on the list of cell magic-supported languages, necessitating the need for a C++ kernel in Jupyter Notebook. This is where Cling kicks in – an interactive C++ “interpreter” which utilises just-in-time (JIT) compilation. It supports Jupyter Notebook by providing a Cling kernel, where all the C++ code is being interpreted/ compiled (the boundary is getting really blurry, eh?).

In this post, we will walk through the process of installing Cling kernel into Jupyter Notebook. The content is largely based on what Shuvomoy Das Gupta wrote, though recent changes see me hitting into problems as the directories are renamed, hence the update. The installation process worked for my OS X 10.11 and CERN provides binary files for Ubuntu, Fedora, CentOS and CoreOS (for ARM64) – not sure if it will work on non-UNIX based systems though.

 

Xcode (OS X only)

For OS X (i.e. apple) systems, we will need the full Xcode installation (not the Xcode command line utility), or our installed kernel will fail to link some libraries and die immediately. If you are unsure, go on AppStore and have a look – it is free to install anyway. The reason why Xcode command line utility alone is insufficient, though actively discussed here, remains unknown.

 

Anaconda, Jupyter Notebook, and GCC

Click here to download Anaconda, a data science platform in python with package management capabilities. Within Anaconda, we can install various packages – either interactively via Anaconda Navigator, or command line interface – without messing up our existing python distribution.

We will need the following packages: jupyter (and its dependencies), gcc and libgcc.

Note: It is always a good idea to create a new environment for these purposes – an existing non-root environment with all these packages installed also does the trick.

 

Installing Cling Kernel

We will need a terminal for the following steps.

Note: If you decided to install under the anaconda environment named “example”, activate the environment beforehand by issuing the command:

source activate example

Click here to download the latest compressed file.

Decompress the file and move it to our favourite location (that is, anywhere you like, for illustration let’s say the directory is now in /home/me/cling/)

Add the path of the binary file to system paths, so they can be found when we run C++ code:

export PATH=/home/me/cling/bin:$PATH

Install the kernel:

cd /home/me/cling/share/cling/Jupyter/kernel
pip install -e .

Register the Cling kernel’s spec on Jupyter:

jupyter-kernelspec install --user cling-c++11

(cling-c++11 can be replaced by cling-c++14 or cling-c++17, depending on which version of C++ we would like to use. The functionality is made available in early Nov 2016.)

… and we are done! If we launch

jupyter notebook

we should be able to choose C++11 under the “new” button on the top-right corner, and create a page which takes C++ code.

 

Before you go with your new toy, read how Shuvomoy Das Gupta creates a C++ function and interacts with OpenCV, and John Foster’s blog post on how he solves a system of linear equations using a C++ library on Jupyter Notebook.

4 thoughts on “Running C++ code in Jupyter Notebook”

  1. Hi Bryan,

    I am following your instruction and after starting a c++ kernel in jupyter notebook, I am getting the following error message

    “`
    whichCling = os.readlink(shutil.which(‘cling’))
    TypeError: readlink: can’t specify None for path argument
    “`

    Do you happen to know how to fix it? Thanks

    1. Hi Ethen,

      I have never encountered this error before while setting my machine up!
      Do you have your environment’s details around (OS, python version) so I can try to replicate the error?

      Bryan

    1. It is indeed. Though I see it this way – won’t including a script to run and test (as opposed to typing something inside Jupyter) defeats the purpose of trying things out in a REPL paradigm?

Leave a Reply

Your email address will not be published. Required fields are marked *