Evgenii Legotckoi
June 18, 2018, 2:01 p.m.

C ++ scripts

Content

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:

  1. #include <stdio.h>
  2. 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:

  1. #include <stdio.h>
  2.  
  3. void _01_hello_world() {
  4. printf("foo\n");
  5. }

... or C ++ version

  1. #include <iostream>
  2.  
  3. void _02_hello_world()
  4. {
  5. std::cout << "Hello world" << std::endl;
  6. }

The examples are pretty simple, but they show you where to start.

What about Qt?

  1. #include <QtWidgets/qapplication.h>
  2. #include <QtWidgets/qpushbutton.h>
  3.  
  4. void _03_basic_qt()
  5. {
  6. int argc = 0;
  7. QApplication app(argc, nullptr);
  8.  
  9. QPushButton button("Hello world");
  10. QObject::connect(&button, &QPushButton::pressed, &app, &QApplication::quit);
  11. button.show();
  12.  
  13. app.exec();
  14. }

But this code won't work out of the box - you need to pass some custom parameters to Cling:

  1. 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.

By article asked0question(s)

2

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • AK
    April 1, 2025, 11:41 a.m.
    Добрый день. В данный момент работаю над проектом, где необходимо выводить звук из программы в определенное аудиоустройство (колонки, наушники, виртуальный кабель и т.д). Пишу на Qt5.12.12 поско…
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup