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?
- Learning C++
One use case of cling is to aid the C++ learning process. Offering imediate feedback the user can easily get familiar with the structures and spelling of the language.
- Creating scripts
The power of an interpreter lays as well in the compactness and ease of repeatedly running a small snippet of code - aka a script. This can be done in cling by inserting the bash-like style line:
#!/usr/bin/cling
- Rapid Application Development (RAD)
Cling can be used successfully for Rapid Application Development allowing for prototyping and proofs of concept taking advantage of dynamicity and feedback during the implementation process.
- Runtime-Generated Code
Sometime it's convenient to create code as a reaction to input (user/network/configuration). Runtime-generated code can interface with C++ libraries.
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.