One of the authors of the Qt Blog shared his experience of using the Cling utility. To solve everyday problems, he needed to write several scripts, and he used C ++ as a scripting language!!! To do this, he used the Cling utility, which is a C++ interpreter based on the Clang compiler and created by CERN.
Cling allows developers to write scripts using C and C++. Because it uses the Clang compiler, it supports the latest versions of the C++ standard. If you execute the interpreter directly, you will have a live runtime where you can start writing C++ code. As part of the standard C/C++ syntax, you'll find several other commands beginning with "." (Dot).
When you use the interactive interpreter, you can write code like:
#include <stdio.h> printf("hello world\n");
As you can see, there is no need to worry about areas; you can just call the function.
If you are planning to use Cling as an interpreter for your scripts, you need to wrap everything inside a function. The default entry point of a script is the same as the filename. It can be configured to call a different function. So the previous example would turn into something like:
#include <stdio.h> void _01_hello_world() { printf("foo\n"); }
... or C ++ version
#include <iostream> void _02_hello_world() { std::cout << "Hello world" << std::endl; }
The examples are pretty simple, but they show you where to start.
What about Qt?
#include <QtWidgets/qapplication.h> #include <QtWidgets/qpushbutton.h> void _03_basic_qt() { int argc = 0; QApplication app(argc, nullptr); QPushButton button("Hello world"); QObject::connect(&button, &QPushButton::pressed, &app, &QApplication::quit); button.show(); app.exec(); }
But this code won't work out of the box - you need to pass some custom parameters to Cling:
cling -I/usr/include/x86_64-linux-gnu/qt5 -fPIC -lQt5Widgets 03_basic_qt.cpp
You can customize your "cling" in a custom script based on your needs.
You can also download Cling as a library in your applications to use C++ as a scripting language.