Building a C++ Interpreter (Cling) for x86 and ARM

Gallagher PryorC/C++ Leave a Comment

A C++ interpreter compiled for ARM running on an x86 EC2 instance after following the given instructions. As C++ becomes more mature and high-level, an interpreted workflow might lead to mainstream C++ productivity in addition to development!

Development through a C++ interpreter (Cling) as opposed to a standard compiler is an amazing leap in productivity and a window into the newest features of C++. This post tells you how to get your own bleeding-edge C++ interpreter built right on top of the development version of LLVM. We give you a repeatable procedure via Amazon EC2.

With our prescribed steps in place, you can always have an up-to-date development version of Cling. This allows quick testing and investigation of LLVM’s newest features. You will also possess a development environment as a path to contributing to Cling or learning how Cling’s innards push C++ code along.


Astoundingly, there is such a thing as a C++ interpreter! Thus, if you’re interested in sticking to C++ for speed and efficiency, you’re not confined to the code-compile-run-debug workflow. Living outside such a workflow lends jumps in productivity and a unique angle through which to see C++.

Development of a serious C++ interpreter was undertaken by the wizards at CERN starting at around 1995. (You might recognize this group as searchers for the Higgs Boson). The C++ interpreter was dubbed CINT and was designed to support a major data analysis and visualization package known as ROOT (screenshot below). The system is certainly serious as it is giving researchers a way to investigate the very fabric of the universe!

A ROOT screenshot being driven by a C++ interpreter (CINT). CINT is not a toy implementation - rather it is used to investigate the fabric of the universe.

Over the last 20 years, CINT fell behind the evolving C++ language. In response, individuals at CERN began work on a replacement for CINT dubbed Cling with the strategy of leveraging LLVM to stay up to date with the growing C++ specification. Up until recently, the project has been rather nascent, but now it is at the point where its functionality and build system make Cling worthwhile for use and contribution.

Build instructions for x86

We present a build procedure that leverages Amazon EC2. By using the cloud, you are guaranteed to use the exact Linux version and hardware capabilities that we did in testing. We assume that you have an Amazon EC2 account. The expected compute resource cost will be around two dollars.

  1. Create an m3.xlarge instance with the Debian Jessie AMI (ami-aae18fc2). Be sure to grab at least 32Gb of storage.

  2. Boot the instance and login as admin.

  3. Install required build packages on the instance.

    sudo apt-get update
    sudo apt-get install build-essential git
  4. Use the distributed build script from the Cling project to clone required source code and build an x86 version of Cling with all available cores.

    chmod +x
    ./           # will require a few minutes to finish

    …this might take some time…

    Building cling on an x86 EC2 instance
  5. Take Cling for a spin!

Build instructions for ARM

Whereas an x86 build is readily supported by the Cling developers, building for ARM is not a standard build path. Thus, ARM build instructions are a bit more involved.

  1. Log in as admin to the (same x86) instance described.

  2. First, you’ll need LLVM and Clang as they provide utilities for compilation: llvm-tblgen and clang-tblgen.

    sudo apt-get install llvm clang
  3. Install ARM hard-float compilation tools.

    sudo apt-get update
    # add Embedian as an installation source
    sudo bash -c 'echo deb jessie main > \
    sudo apt-get install -y curl
    curl | sudo apt-key add -
    # add arm hard-float as a supported architecture
    sudo dpkg --add-architecture armhf
    sudo apt-get update
    # install an ARM targeted cross compiler
    sudo apt-get install crossbuild-essential-armhf
  4. Install QEMU-based ARM emulation. This is a fantastic feature of newer linux kernels: ARM executables are registered with the system so that under a normal invocation, QEMU is invoked under the hood so that it seems as though ARM binaries are native.

    # install QEMU emulation for ARM executables
    sudo apt-get install qemu binfmt-support qemu-user-static
    sudo update-binfmts --display
    sudo apt-get install libc6:armhf
    # compile an ARM executable
    cat > hello.c << EOF
    int main(void) { return printf("Hello ARM!\n"); }
    arm-linux-gnueabihf-gcc -static -ohello hello.c
    # the following should indicate an ARM exe
    file hello
    # running the executable with no fanfare should
    # transparently invoke QEMU to print "Hello ARM!"

    Having ARM emulation on our fast build machine makes debugging amazingly faster.

  5. Install build tools: cmake and ninja.

     # install cmake
     sudo apt-get install cmake
     # ninja requires a build
     git clone git:// && cd ninja
     git checkout release
     ./ --bootstrap
     chmod +x ninja
     sudo cp ninja /usr/bin
  6. Clone LLVM, Clang, and Cling to the llvm directory. This directory differs from the instructions, above, so no worries if you’re following these steps in the order of the blog post. Note the specific SHA’s we jump to in the repositories. As of this writing, ARM support is in and out, but will hopefully materialize for good!

    # the following steps assume this is done on a large volume
    # mounted at /mnt resulting in /mnt/llvm.
    git clone
    cd llvm
    git checkout 88c56cd6d6aec0c44e9ca5aa96b354c1261a95aa
    cd tools
    git clone
    cd cling
    git checkout 899a5dde32cf670605a1478bea0b2d6fd3f95bef
    cd ..
    git clone
    cd clang 
    git checkout d649bf1a04be728fc09222d111e696fce4b55324
  7. Configure a build in a new build directory using cmake. Note the intense and carefully crafted build flags required to do a successful cross compilation. This took quite a bit of debugging to lock down.

    Note that the following assumes that the prior step was run in /mnt resulting in an /mnt/llvm directory. Indeed, the following block assumes that it is run in the /mnt directory, as well.

    mkdir -p build; cd build
    cmake -G Ninja /mnt/llvm -DCMAKE_CROSSCOMPILING=True \
                      -DCMAKE_SYSTEM_NAME=Linux \
                      -DCMAKE_SYSTEM_VERSION=1 \
                      -DCMAKE_SYSTEM_PROCESSOR=armv7 \
                      -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \
                      -DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ \
                      -DCMAKE_INSTALL_PREFIX=/mnt/build/install \
                      -DLLVM_TABLEGEN=/usr/bin/llvm-tblgen-3.5 \
                      -DCLANG_TABLEGEN=/usr/bin/clang-tblgen-3.5 \
                      -DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf \
                      -DLLVM_HOST_TRIPLE=arm--linux-gnueabihf \
                      -DLLVM_TARGET_ARCH=ARM \
                      -DLLVM_TARGETS_TO_BUILD=ARM \
                          -mcpu=cortex-a9 \
                          -I/usr/include/arm-linux-gnueabihf/c++/4.9 \
                          -I/usr/lib/gcc/arm-linux-gnueabihf/4.9/include \
  8. Do a build with ninja and install to the install subdirectory. This will take a few minutes.

    # from the /mnt/build directory
    mkdir -p install
    ninja install
  9. Fire up Cling! You can do this directly on your Amazon EC2 instance through QEMU, but watch out – it is a tad unstable in the case of recovering from syntax errors, etc. However, you can copy what you built to an ARM machine such as a Raspberry Pi or Cubox to take it for a whirl there, too!

  10. cd /mnt/build/install/bin

Leave a Reply

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