# Augmenting Cling with Custom Functions

**URL:** <https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250>\
**Category:** Newbie\
**Created:** [January 24, 2023, 7:34pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250 "2023-01-24T19:34:20Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [January 24, 2023, 7:34pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/1 "2023-01-24T19:34:20Z")

</div>

I had asked about building a custom interpreter using root in [this post](https://root-forum.cern.ch/t/building-custom-interpreter-in-root/52988) .

I was pointed to [this cling-demo](https://github.com/root-project/cling/tree/master/tools/demo) as a possible solution.

I downloaded and built cling and built the cling-demo. However, what it does is not what I am looking for. The cling-demo is an executable that uses the interpreter functionality but cannot interpret a script file that is passed to it as an argument.

I am trying to build an ‘enhanced’ interpreter that integrates additional proprietary functionality into cling which can then be used to interpret a source file which does not need to be compiled.

I’m starting this new thread to see if I can explain better and build what I need using Cling (or Root).

I hope that someone familiar with both Cint and Cling/Root can tell me if what I am looking for is possible in Cling/Root (and how). Cint worked great for us and we wouldn’t be looking for another solution had support for Cint not ceased for recent releases of linux.

To reiterate, I just need an augmented C++ interpreter which can also execute some other proprietary instrumentation functions that I have.

More given below with a very trivial code example.

Thank you.

\_ROOT Version: v6.26.10  
\_Platform: Ubuntu 22.04  
\_Compiler: g++ 11.3.0

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [January 24, 2023, 7:50pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/2 "2023-01-24T19:50:18Z")

</div>

I have a source file **mysrc.c** which implements this function _myfunc1()_ that returns the product of the two input parameters:

```auto
int myfunc1(int x, int y)
{
   return (x * y);
}

```

I’d like to integrate _myfunc1()_ into cling/root so that the new cling/root can interpret a script file **myscript.c** which calls _myfunc1()_. The aim is to not need any compilation for **myscript.c** to be able to use the functions that it invokes.

```auto
/* File myscript.c */

#include <stdio.h>

main()
{
  int x = myfunc1(3, 4);
  printf("%d\n", x);
}

```

To put it in Cint perspective, I could accomplish the above as follows with Cint:

1. Declare the function prototype of _myfunc1()_ in **myhdrs.h**
2. Compile **mysrc.c** to get **mysrc.o**
3. Define a makecint makefile with the following command -

```auto
makecint -mk mkfile_mylib -o mycint -H myhdrs.h -l mysrc.o

```

The above creates a makefile called **mkfile\_mylib**

When ‘_make -f mkfile\_mylib_’ is executed, it creates the augmented/custom Cint _mycint_.

Then, _mycint_ can be used by itself (no arguments) and it starts the augmented  
interpreter which can also execute myfunc1() as follows (“$\>” is the shell prompt):

```auto
$> 
$> mycint
mycint>
mycint> {myfunc1(2, 3);}
(int)6
mycint>
mycint> q
$>

```

or I could pass the script file **myscript.c** to _mycint_ as follows:

```auto
$> 
$> mycint myscript.c
$> 12
$>

```

Is it possible to accomplish this with Cling (or root)? If so, how?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 25, 2023, 6:27am UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/3 "2023-01-25T06:27:47Z")

</div>

I guess @vvassilev and @Axel can help you.

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [January 25, 2023, 2:40pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/4 "2023-01-25T14:40:11Z")

</div>

Yes, I hope one of the experts gives me some pointers. Axel N, Daniel B and Philippe C had provided invaluable guidance and workarounds when I built the custom cint many years back.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 25, 2023, 2:44pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/5 "2023-01-25T14:44:31Z")

</div>

[Vassil Vasisilev](https://root.cern/about/team/#Vassil%20Vassilev) @vvassilev is the author of the cling demo you pointed out in your first post. I am sure he can help you.

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [January 28, 2023, 8:51am UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/6 "2023-01-28T08:51:59Z")

</div>

Hi @salz-root, apologies for the delay. Have you tried to build cling standalone from [GitHub - root-project/cling: The cling C++ interpreter](https://github.com/root-project/cling/) ?

Then, instead of building a `.o` file you could build a `.so` file. Then from the cling prompt you can do:

```cpp
cling> #include "myhdrs.h"
cling> .L myLib.so
cling> myfunc1(2, 3);

```

Is that the use case you had in mind or I am misinterpreting your project requirements?

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [January 29, 2023, 5:07pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/7 "2023-01-29T17:07:25Z")

</div>

Thank you @vvassilev for responding.

I have not built _cling_ from the URL you have provided but I built it using the following directives from the [cling\_build\_instructions page](https://root.cern/cling/cling_build_instructions) :

```auto
git clone http://root.cern/git/llvm.git src
cd src
git checkout cling-patches
cd tools
git clone http://root.cern/git/cling.git
git clone http://root.cern/git/clang.git
cd clang
git checkout cling-patches
cd ../..

mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX=[Install Path] -DCMAKE_BUILD_TYPE=Release ../../src
cmake --build .
cmake --build . --target install

```

My questions -

1. Do I need to discard the _cling_ I have built and rebuild it from the [root-project/cling](https://github.com/root-project/cling/) page that you’ve referred to?

2. What is the command syntax to build the shared library **mylib.so** from **mysrc.c**?

Currently, I can load the source file into the _cling_ that I have built and do the following:

```auto
[cling]$ #include "myhdrs.h"
[cling]$ .L mysrc.c
[cling]$ myfunc1(2, 3)
(int) 6
[cling]$

```

But we won’t be sharing the source files such as **mysrc.c** with anyone since they contain proprietary code. So we need to create libraries from our source and integrate them into _cling_ beforehand. The end user just has to write a source script ( **myscript.c** ) which would invoke functions that are listed in **myhdrs.h** and pass the script file to the modified _cling_ that already has the library of our proprietary functions built in. The end user would not be loading any libraries (e.g., with a “.L mylib.so” command in _cling_) to be able to execute/interpret their scripts that call our proprietary functions.

Please see my earlier post where I described how we achieved our project goals with cint.

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [January 29, 2023, 9:16pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/8 "2023-01-29T21:16:46Z")

</div>

> [@salz-root](#):
>
> Do I need to discard the _cling_ I have built and rebuild it from the [root-project/cling](https://github.com/root-project/cling/) page that you’ve referred to?

As long as you can run `cling` then it does not matter where it came from.

> [@salz-root](#):
>
> What is the command syntax to build the shared library **mylib.so** from **mysrc.c**?

`gcc -o mylib.so -shared mysrc.c`?

> [@salz-root](#):
>
> Currently, I can load the source file into the _cling_ that I have built and do the following:
> 
> ```auto
> [cling]$ #include "myhdrs.h"
> [cling]$ .L mysrc.c
> [cling]$ myfunc1(2, 3)
> (int) 6
> [cling]$
> 
> ```

You just need to do `.L mylib.so`.

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [January 30, 2023, 10:23pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/9 "2023-01-30T22:23:56Z")

</div>

Thank you again @vvassilev.

I am finally able to build a shared library both with source code files and pre-compiled object files and load it into cling. This is a library built with multiple files calling functions defined in different places. So I am happy to have made it thus far. I can execute some simple functions that are built into the shared library.

However, I get segmentation fault the moment I try “anything of value”, i.e., anything that involves port initialization etc., which are essential for our tools. I cannot see anything in the stack dump that would point me in a helpful direction. The source code I am using is time tested and should not have segfaults. So, I am probably missing some vital piece of information.

What is PIC? And what does “-fPIC” do? While trying to build my library, I once got an error message that suggested I recompile with “-fPIC”. I have been unable to find what that is. But the error went away after I compiled as suggested.

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [January 31, 2023, 7:50pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/10 "2023-01-31T19:50:00Z")

</div>

> [@salz-root](#):
>
> However, I get segmentation fault the moment I try “anything of value”, i.e., anything that involves port initialization etc., which are essential for our tools. I cannot see anything in the stack dump that would point me in a helpful direction. The source code I am using is time tested and should not have segfaults. So, I am probably missing some vital piece of information.

Can you provide a minimal (obfuscated if you are more comfortable that way) reproducer where I can take a look?

> [@salz-root](#):
>
> What is PIC? And what does “-fPIC” do? While trying to build my library, I once got an error message that suggested I recompile with “-fPIC”. I have been unable to find what that is. But the error went away after I compiled as suggested.

Position independent code: [Position-independent code - Wikipedia](https://en.wikipedia.org/wiki/Position-independent_code)

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [January 31, 2023, 10:53pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/11 "2023-01-31T22:53:28Z")

</div>

I realized later that since the PIC error was seen while building my library, it had nothing to do with _cling_ and was from g++. Sorry about that. But thanks for the Wiki pointer. The error was in relation to some open source AES encryption code we incorporate with the code we have written. I had not seen this error before while compiling with previous versions of g++. So I am not sure what causes the error now as the source code hasn’t changed (but I’ll leave it for now since I could build the library).

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [January 31, 2023, 11:06pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/12 "2023-01-31T23:06:19Z")

</div>

Regarding the segfault, I will see if I can reproduce it with something that I can share.  
Meanwhile, I noticed something and wonder if that could be a cause.

If I have a file like **mytest.c**

```auto
/* File mytest.c */

#include <stdio.h>

main()
{
  printf("\nHello from main()\n");
}

```

_cint_ could execute it without any error/warning because it looks for main(). In other words, the following

```auto
cint mytest.c

```

prints “Hello from main()”. The return type of main() can be omitted where main() is defined.

g++ only gives a warning but also compiles the above **mytest.c** and produces the same output as _cint_.

```auto
$>
$> g++ -o mytest mytest.c
$> ./mytest
$>
$> Hello from main()
$>

```

With _cling_ (and _root_), main() seems to have no significance and the function in **mytest.c** must be renamed to mytest() and explicitly defined to be of type **void** as follows:

```auto
void mytest()

```

so that

```auto
cling mytest.c

```

works (i.e., prints “Hello from main()”).

But the more serious problem is that functions of implicit return type **int** like this:

```auto
mytest()
{
  printf("\nHello from main()\n");
  return 0;
}

```

or a function where the return type **int** is specified but which has no return statement like this

```auto
int mytest()
{
  printf("\nHello from main()\n");
  // return 0;
}

```

both result in a core dump when invoked with _cling_.

```auto
cling mytest.c

```

We probably have several functions of type **int** that may not have a return statement or have an implicit return type of **int**.

Could this be causing the segfault?

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [February 3, 2023, 3:07pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/13 "2023-02-03T15:07:13Z")

</div>

@vvassilev, is there any way to direct _cling_ to execute a certain function when it starts up? A command line option or some config file parameter?

_makecint_ had a flag (-B) to specify the initialization function while building the custom _cint_, so that when the custom _cint_ was invoked, it executed the specified initialization function before the interpreter prompt was seen.

Thank you.

---

<div class="post-metadata">

**Author:** ![vvassilev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/f0a364/32.png) [@vvassilev](https://root-forum.cern.ch/u/vvassilev)\
**Post date:** [February 5, 2023, 11:19am UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/14 "2023-02-05T11:19:34Z")

</div>

You can do `.x filename.C` which would load `filename.C` and run a function called `filename`.

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [February 10, 2023, 10:36pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/15 "2023-02-10T22:36:06Z")

</div>

@vvassilev,

In our setup, we do not use the customized interpreter (with our proprietary functions) by itself where one could do `.x filename.c` etc. **after** starting the interpreter. Expecting the end user to type anything extra will not work well.

Anything that a user does not need to know or do is built into the customized interpreter via incorporating the shared library (say, **mylib.so** ) which is built with our functions. The intent is to allow the user to be able to execute their own scripts that invoke our proprietary functions to do their testing easily in addition to executing the test suites we provide.

Consider that (hypothetically) our end users want to test an automotive engine. They want to be able to change the RPM, change the amount of fuel injected, read the engine temperature under various conditions, etc. We provide API functions like _set\_rpm()_, _set\_fuel\_input()_. They don’t know what happens inside _set\_rpm()_, _set\_fuel\_input()_, etc. But they could write a test script **rpm\_test.c** which would, say, increase the RPM progressively via _set\_rpm()_ till the engine explodes or something (bad design) 🙂 or shuts down safely.

With an interpreter that has _set\_rpm()_, _set\_fuel\_input()_ etc. loaded through the shared library ( **mylib.so** ), they can easily execute **rpm\_test.c** and don’t need the source code for _set\_rpm()_.

The only file that a user would execute is **rpm\_test.c**. However, before that, the initialization function I was asking about earlier would crank the engine and maybe do some other safety checks so that the setup is ready for **rpm\_test.c**. The end users don’t need to concern themselves with the details of that.

We provide lots of tests like **rpm\_test.c** in our test suite which assume that the initialization tasks have been performed. Unless initialization is successful, no test can be run.

Currently, I am able to do the following -

```auto
cling -l mylib.so rpm_test.c

```

where **rpm\_test.c** has a function _rpm\_test()_ that calls the initialization function built into **mylib.so** and then calls _set\_rpm()_. But this involves modifying our pre-existing **rpm\_test.c** to first call the initialization function before doing other RPM related testing. That would require a prohibitive amount of code change when nothing actually needs to change in **rpm\_test.c** and other test scripts. We were able to use all those scripts with the custom _cint_ which had a facility to specify an initialization function that would get executed upon invocation of the custom _cint_.

I hope this explains the requirements somewhat better. I have tried various other things to invoke the initialization function with _cling_ without changing _rpm\_test.c_ but have not had success. Please let me know if there is a way to accomplish this with _cling_ without massive code overhaul.

Also, could you please take a look at my earlier post (Jan 31) about the conditions under which I saw core dumps? I provided code examples (file **mytest.c** ) that would help you reproduce the problem.

Thank you.

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [March 25, 2023, 12:14am UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/16 "2023-03-25T00:14:42Z")

</div>

Hi @salz-root,

I wanted to follow-up on this topic. Did you make any progress at all?

> [@salz-root](#):
>
> In our setup, we do not use the customized interpreter (with our proprietary functions) by itself where one could do `.x filename.c` etc. **after** starting the interpreter. Expecting the end user to type anything extra will not work well.

The two immediate solutions that I can think of is, either (1) you modify `cling` itself to execute a startup script; or (2) you modify the cling demo to parse an external source file. If you also need an interactive prompt, option (1) seems more adequate and it should be simple enough to tweak cling to your requirement.

> [@salz-root](#):
>
> Currently, I am able to do the following -
> 
> ```auto
> cling -l mylib.so rpm_test.c
> 
> ```

Given that, I think all that is missing is injecting a `.x` command before the REPL loop starts, no? A simple way, would be to modify `UserInterface.cpp`, and add the following before the [`while` loop in line 148](https://github.com/root-project/cling/blob/master/lib/UserInterface/UserInterface.cpp#L148):

```c++
  {
    cling::Interpreter::CompilationResult result;
    m_MetaProcessor->process(".x /path/to/.cling_startup.cpp", result);
  }

```

where in `/path/to/.cling_startup.cpp` you would add your startup code. Such file might include any preprocessor directive, e.g. `#include`, top-level declarations, and an additional function `void _cling_startup()` that will be automatically called on startup.

Hope this helps in case you didn’t found your way through already.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [March 25, 2023, 2:04pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/17 "2023-03-25T14:04:14Z")

</div>

A bit related to the return type issues and cling: [[cling] void macro should not return value · Issue #10895 · root-project/root · GitHub](https://github.com/root-project/root/issues/10895)

---

<div class="post-metadata">

**Author:** ![salz-root](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@salz-root](https://root-forum.cern.ch/u/salz-root)\
**Post date:** [March 28, 2023, 10:07pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/18 "2023-03-28T22:07:15Z")

</div>

Thank you @jalopezg .

I currently have a workaround that enables me to do most of what I need without modifying _cling_. That is true if I am executing a script but not when I use cling in the interactive format. The latter requires a #include directive before we can use any of the functions in our library that we load into cling.

I will experiment with building cling with your suggestion #1. The cling demo is not something that will work for our needs.

Could you please point me to where the _cling_ prompt is defined? That is, when I invoke _cling_, I’d like the prompt to have a different string than “[cling]$”. Like the shell prompt PS1 in linux.

It would be great if _cling_ could execute main() if it couldn’t find a function that has the same name as the source script that is passed to it to interpret. All our test scripts have main().

Thank you.

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [March 29, 2023, 5:02pm UTC](https://root-forum.cern.ch/t/augmenting-cling-with-custom-functions/53250/19 "2023-03-29T17:02:17Z")

</div>

> [@jalopezg](#):
>
> Given that, I think all that is missing is injecting a `.x` command before the REPL loop starts, no? A simple way, would be to modify `UserInterface.cpp`, and add the following before the [`while` loop in line 148](https://github.com/root-project/cling/blob/master/lib/UserInterface/UserInterface.cpp#L148):
> 
> ``

For the cling prompt, see the link provided in my previous post (cited); it’s literally 2 lines above the referenced line.

Cheers,  
J.
