Oscar Forner Martinez bio photo

Oscar Forner Martinez

I am software engineer at VCA Technology. Learning Linux Kernel, passionate about GNU/Linux and OSS, enjoying algorithms and data structures and coding in C and C++.

Email Twitter Google+ LinkedIn Github

Cling is an interactive C++ interpreter, built on top of Clang and LLVM compiler infrastructure. It can be found in GitHub (Note: Lately there has not been that much development on it).

Since I use Arch Linux I am lucky to have cling in the AUR repository (cling-git). For other ones you can use the cling-all-in-one repo that contains a script to download all dependencies, compile and build cling for you.

As always, all the code used in this post is available in this repo

The videos are made with asciinema, that means you can copy from the video.

The basics of Cling

In this section I will show the basic commands we can run on cling besides C/C++ code.

Command Explanation
.I path to add includes. Like -I option at compiling time
.L file to load file. This is important to try classes or functions that you already have
.rawInput Toggle wrapping and printing the execution results of the input
.help Shows a nice help with these and other commands that can be used

Examples of usage

In this section examples of usage using the basics from the previous section will be shown.

Load an existing file with a function we want to test

In this first example the following header will be loaded to use it:

The header above can be used from Cling as shown in the following video:

Include a folder and load a template class from that folder

This example will include a folder that contains a class that will be loaded from Cling:

The class above can be loaded from Cling as shown in the following video:

Write a function on-the-fly

Cling allows us to write functions or classes on-the-fly:

Conclusion

I do not consider Cling as my main way to work in C/C++, but it is my first option when I want to try something quick in a function or a class that I am interested in using. It is not a perfect tool for everything, but it is faster than setting up a whole example when you are only interested in something specific.