Cling interprets C++


****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <string>
[cling]$ std::string s("abc");
[cling]$ s.find('b')
(std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size_type) 1
[cling]$

Cling is built on the top of LLVM and Clang libraries. In addition to standard interpreters it has a command line prompt and uses just-in-time (JIT) compiler. This kind of software application is commonly known as an interactive compiler.
Cling started off as a contemporary, high-performance alternative of the current C++ interpreter in the ROOT project - CINT.

Why interpreting C++ with Cling?

Embedding Cling

The functionality of an application can be enriched by embedding Cling. To embed Cling, the main program has to be provided. One of the things this main program has to do is initialize the Cling interpreter. There are optional calls to pass command line arguments to Cling. Afterwards, you can call the interpreter from any anywhere within the application.

For compilation and linkage the application needs the path to the clang and llvm libraries and the invocation is order dependent since the linker cannot do backward searches.


g++ embedcling.cxx -std=c++11 -L/usr/local/lib
                    -lclingInterpreter -lclingUtils 
                    -lclangFrontend -lclangSerialization -lclangParse -lclangSema 
                    -lclangAnalysis -lclangEdit -lclangLex -lclangDriver -lclangCodeGen 
                    -lclangBasic  -lclangAST  
                    `llvm-config 
                      --libs bitwriter mcjit orcjit native option 
                        ipo profiledata instrumentation objcarcopts` 
                      -lz -pthread -ldl -ltinfo 
                    -o embedcling
        

Embedding cling requires the creation of the interpreter. Optionally compiler arguments and the resource directory of llvm can be passed. An example is the following:


#include "cling/Interpreter/Interpreter.h"

int main(int argc, char** argv) {
  const char* LLVMRESDIR = "/usr/local/"; //path to llvm resource directory
  cling::Interpreter interp(argc, argv, LLVMRESDIR);

  interp.declare("int p=0;");
}
        

A more complete example could be found in tools/demo/cling-demo.cpp

Download

We are developing Cling according to the principle of Release early and release often. Binaries are available for download.

Support

Support is provided through a fast-response forum, where questions of all levels are welcomed. Queries can also be sent to our mailing list: cling-dev@cern.ch.