# Embedding cling in a compiled C++ program

**URL:** <https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038>\
**Category:** Cling\
**Created:** [March 11, 2017, 4:06am UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038 "2017-03-11T04:06:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![artsera](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@artsera](https://root-forum.cern.ch/u/artsera)\
**Post date:** [March 11, 2017, 4:06am UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/1 "2017-03-11T04:06:56Z")

</div>

Hello folks,

I am writing a rules engine for an AI module. The module will store and retrieve rule sets from a database. The intention is for the module to edit/enhance/correct rule sets which will then be executed by the engine. The engine would launch a thread which in turn would create a cling interpreter instance to execute the rule set. In order for it to work correctly, the C++ module needs to pass a context object instance to the cling script and the script needs to return a result object back to the C++ module.

As I said, the cling script (a c++ object and associated methods constituting the rule engine) would receive the rule set and the context object, execute the rules, and then return a result object back to the compiled caller.

Can this be done? I’m looking for the proper set and sequence of API calls to accomplish this objective.

Thanks,

Arthur Sera

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 11, 2017, 9:35am UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/2 "2017-03-11T09:35:41Z")

</div>

Hi Arthur,

Cling sounds like a perfect match here, this is exactly what it was designed to do.

Code is passed to cling as a string. You can use `Interpreter::evaluate` to receive the object back as a `Value`, or even simpler, you use the same approach you could use for passing values into cling: passing the address of a variable, that the code run inside cling will access, eg using `Interpreter:: process`. Example code snippet:

```auto
int out = 42;
int in = 17;
stringstream sscode;
sscode << "*((int*)" << &out << ") = *((int*)" << &in << ") * 2";
interp.process(sscode.str());

```

Now, `out` is 34.

We’ll be happy to provide more detailed answers should you have specific questions!

Axel

---

<div class="post-metadata">

**Author:** ![artsera](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@artsera](https://root-forum.cern.ch/u/artsera)\
**Post date:** [March 15, 2017, 6:47pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/3 "2017-03-15T18:47:05Z")

</div>

Hello Axel,

As it happens, I have run into what I believe are documentation obstacles. By that I mean that I cannot find something similar to we used to call a programmers guide. This was a document that laid out both the concepts of the language,utility or whatever it was, and the gross implementation details.

If there were such a beast for ‘Cling’, I would expect to be able to find a section called something like ‘Embedding Cling into your program’. In that section would be found a simple explanation of what header files to use, the principal functions to be called to instantiate the interpreter, and what libraries to link with for static or dynamic linking of the user’s program.

I hate wasting the time of busy gurus but I would greatly appreciate it if you could point me to a source for answering the following questions for my intended use cases:

1. What header files do I need to include in my C++ compiled program to access the Cling interpreter’s needed data structures?

2. What libraries and in what order will I need to link the Cling interpreter to my C++ compiled program?

3. My C++ program will make extensive use of the Posix thread facilities. I intend to manage a pool of worker threads that will each independently utilize the Cling interpreter to run C++ scripts. The worker thread pools will be managed by schedulers whose services are enlisted by creating requests that are appended to the schedulers request queues. To facilitate inter-thread communications, the interpreter threads will lock a schedulers mutex, add to their request queue, and then wakeup the scheduler thread my signaling with its condition variable. Therefore, the interpreter scripts will need:

Finally, it would be very nice if I were able to reset the interpreter instance back to its initial state after successfully completing the primary script so I did not have to fully construct a new instance of the interpreter before returning the work thread back to the available list in the worker thread pool.

Thanks for your good works and your assistance,

Arthur Sera

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 15, 2017, 7:04pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/4 "2017-03-15T19:04:57Z")

</div>

Hi Arthur,

Fair enough, I see your point.

Would a sample program suffice? We had another request for a CMakeFile.txt that shows how to build a project using cling as a library; this could take care of both.

Would that be an acceptable solution?

The problems I have with documents is that they are even more annoying to write than code 😉 and they are more ambiguous to read than code and they are less maintainable (because no compiler ever complains).

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![artsera](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@artsera](https://root-forum.cern.ch/u/artsera)\
**Post date:** [March 15, 2017, 7:15pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/5 "2017-03-15T19:15:32Z")

</div>

Thanks Axel,

I’m always happy to have a good example. I agree with your comments about the fact that writing documentation tends to be an annoyance. I have always tried to get management to budget for a good technical writer to assist with these burdensome tasks. Still, I know it can still be painful to have to slow down and explain things to the tech writer so he/she can write meaningful prose that embraces the meaning and elegance of the artworks that have been produced.

Cheers & Glass Clinks,

Arthur

---

<div class="post-metadata">

**Author:** ![artsera](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@artsera](https://root-forum.cern.ch/u/artsera)\
**Post date:** [March 15, 2017, 7:20pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/6 "2017-03-15T19:20:27Z")

</div>

Axel,

You know that while I can enjoy a good read about theory and operations, I could get by with just a brief answer to 2 questions:

1. What header files do I need for embedding and what libraries need to be linked with my C++ complied code?
2. How do I get cling to load ‘[libpthread.so](http://libpthread.so)’ or any other external shared lib?

Thanks again,

Arthur

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 15, 2017, 7:44pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/7 "2017-03-15T19:44:44Z")

</div>

Hi,

Look at cling/tools/driver/cling.cpp; it shows what a binary using cling can look like: how to create an `Interpreter`, which headers to `#include`.

Its CMakeLists.txt shows what to link against. But most of the clang and llvm libraries are pulled in as dependencies of libclingInterpreter, libclingUtils etc - depending on what you use (MetaProcessor? Or just the Interpreter?) you might need different libraries.

Have a look at cling/lib/Interpreter/CMakeLists.txt which enumerates these libraries. It’s a bit involved, because this uses the llvm machinery for translating functionality (“native”) into library names. If you use CMake you should be able to use the same approach given their FindLLVM or whatever it’s called. I’ve never done this myself - so it’s not a question of me not sharing knowledge but of me now knowing 🙂 If you have a CMake “recipe” I’d appreciate if you can share it.

Regarding the second part: you can link your binary against libpthread and cling will be able to resolve symbols (specially if you build your binary as -rdynamic, on Linux). You can then simply interpret `#include <pthread.h>` and use its functionality in the interpreter.

You can communicate with the interpreter world either through `cling::Value` or through the address passing I explained above.

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![artsera](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@artsera](https://root-forum.cern.ch/u/artsera)\
**Post date:** [March 16, 2017, 8:44pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/8 "2017-03-16T20:44:15Z")

</div>

I’ve been messing with cling/tools/driver/cling.cpp like you suggested. I’ve been programming since the 70’s and I’ve used make since the late 80’s. However, I’ve never been real attracted to cmake. I guess I should probably see if O’Reilly has a cmake book, but at the moment the CMakeLists.txt file does not enlighten me with regards to what libraries I should use to embed cling in my program.

Even after running cmake for that part of the source tree, I can’t seem to end up with a Makefile that has a clearly defined $(LIBS) variable much less an actual link to an executable statement. Come on. Someone has to have done this.

Sigh!

---

<div class="post-metadata">

**Author:** ![artsera](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@artsera](https://root-forum.cern.ch/u/artsera)\
**Post date:** [March 17, 2017, 3:58am UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/9 "2017-03-17T03:58:22Z")

</div>

Ok, I found an old posting by Axel that helped lead the way. There’s a thread that is called:

How to properly link cling into my executable with cmake

The first post is Oct '15, the last one is Sep '16. The last post says that there is now a “[libcling.so](http://libcling.so)”. So the solution to compiling and linking the program cling/tools/driver/cling.cpp is as follows for me based upon my copying the cling.cpp program to my root build directory (i.e. /src/root-6.08.06/build - the directory where I ran cmake …) and having run “make install” after the 7 day creation exercise (well not really 7 days, but lengthy). Therefore the following was required to build it:

g++ -c -o clingI.o -std=c++11 -Iinclude cling.cpp  
g++ -o clingI cling.o -lclingUserInterface -lcling

Aparently, most everything was stuffed into the [libcling.so](http://libcling.so) library. The library libclingUserInterface.a was necessary because the source file ‘cling.cpp’ instantiates UserInterface (cling::UserInterface ui(interp)😉 to accommodate interactive code entry/execution. I don’t believe I’ll need that for my program but it’s nice to have it available for some other purposes.

If anyone has any questions about this please feel free to ask: [artsera@yahoo.com](mailto:artsera@yahoo.com)

Good Luck,

Arthur Sera

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 17, 2017, 3:27pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/10 "2017-03-17T15:27:12Z")

</div>

Hi,

Indeed, that’s the easiest approach. If you want to avoid the runtime mess of finding the shared library then take the static libraries (.a files), else [libcling.so](http://libcling.so) is the way to go.

Axel.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 19, 2017, 9:51pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/11 "2017-03-19T21:51:11Z")

</div>

…and I have just pushed [cling/tools/demo](https://github.com/root-project/cling/tree/master/tools/demo): a CMake project that shows how to code, compile and link a binary that uses cling. I hope you like it 🙂

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![artsera](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@artsera](https://root-forum.cern.ch/u/artsera)\
**Post date:** [March 27, 2017, 5:12pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/12 "2017-03-27T17:12:21Z")

</div>

Hello Axel,

Thanks for posting the demo. Is there any way for me to call a Compiled function/method from the cling interpreter script?

Thanks,

Arthur Sera

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 27, 2017, 8:43pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/13 "2017-03-27T20:43:46Z")

</div>

Hi Arthur,

Isn’t `get` / `setAnotherGlobal()` just that?

Axel

---

<div class="post-metadata">

**Author:** ![artsera](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a3d4f5/32.png) [@artsera](https://root-forum.cern.ch/u/artsera)\
**Post date:** [March 27, 2017, 10:32pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/14 "2017-03-27T22:32:54Z")

</div>

Hmmm,

I can’t find any method name within the Interpreter class. There’s a getAddressOfGlobal method, but I can’t find a method to set one.

?

Arthur

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 28, 2017, 4:50am UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/15 "2017-03-28T04:50:25Z")

</div>

Hi Arthur,

I was talking about the example in cling/tools/demo which defines this function to demonstrate exactly what you are looking for.

Axel

---

<div class="post-metadata">

**Author:** ![forrest](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/forrest/32/17552_2.png) [@forrest](https://root-forum.cern.ch/u/forrest)\
**Post date:** [February 13, 2019, 11:36pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/16 "2019-02-13T23:36:31Z")

</div>

Because trying to compile cling exhausted my disk space, I only installed the binaries, hence there is no ‘/cling-source-dir/tools/cling/tools/demo’ directory on my machine… What should I do instead to compile this on ubuntu 18.04 ?

---

<div class="post-metadata">

**Author:** ![forrest](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/forrest/32/17552_2.png) [@forrest](https://root-forum.cern.ch/u/forrest)\
**Post date:** [February 16, 2019, 4:13pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/17 "2019-02-16T16:13:29Z")

</div>

It’s not a makefile I need; without the sources it’s not much use. & I’ve got cling up & running fine.

Given that, does anyone know which libraries & headers are needed to compile cling-demo.cpp on Linux?

---

<div class="post-metadata">

**Author:** ![forrest](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/forrest/32/17552_2.png) [@forrest](https://root-forum.cern.ch/u/forrest)\
**Post date:** [February 18, 2019, 7:57pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/18 "2019-02-18T19:57:44Z")

</div>

trying to use cmake I try:

cmake -Dcling\_DIR=ci/inst/lib/cmake/cling ci/src/tools/cling/tools/demo

and get:  
– Configuring done  
CMake Error: CMake can not determine linker language for target: cling-demo  
CMake Error: Cannot determine link language for target “cling-demo”.  
– Generating done  
CMake Warning:  
Manually-specified variables were not used by the project:

```
cling_DIR

```

## – Build files have been written to: /home/forrest

Suggestions?

---

<div class="post-metadata">

**Author:** ![elix22](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@elix22](https://root-forum.cern.ch/u/elix22)\
**Post date:** [November 3, 2020, 4:30pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/19 "2020-11-03T16:30:14Z")

</div>

Hi  
I played with the **cling-demo** sample found in [https://github.com/elix22/cling/tree/master/tools/demo](https://github.com/elix22/cling/tree/master/tools/demo)

Works great .  
I am curious if it’s possible to do the same with C++ Classes.  
Specifically instantiate a native class from the interpreter side and call some of its methods and vice versa .

I am aware of the C++ name mangling issues .  
I am aware that it’s possible to create some extern “C” wrapper functions that will do the job , but I consider it to be an workaround .  
Is there another way ?

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 3, 2020, 4:49pm UTC](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038/20 "2020-11-03T16:49:51Z")

</div>

Hi Eli,

Welcome to the ROOT forum!

Sure, that’s the whole beauty of cling: just `#include` a header in cling, and use the same header in your compiled code, and if the flags are all lined up then you can pass objects from compiled code into the interpreter and back! That’s what ROOT does, in production, 24/7, on 100’000s cores world-wise: it works!

Cheers, Axel.

[Next page](https://root-forum.cern.ch/t/embedding-cling-in-a-compiled-c-program/24038.md?page=2)
