Embedding cling and sharing c++ objects?

Hi all,

I want to be able to embed cling in one of my (g++) cpp programs, and want to be able to modify cpp classes on the fly in a similar manor as how someone would do with a lua cpp binding. I havent been able to find any material on how to do this with cling and was hoping someone could point me in the right direction.

An example of what i want to do [code]
// GCC compiled code
Struct foo {
Int a;
Double d;
Std::string str;
};
Int main (){
Foo bar;

RunThroughInterpreter (Foo & bar, const std::string &src = “”);
// by now, i would like bar to have been modified by a cling instance called in RunThroughInterpreter()
Return 0;
}[/code]
Please forgive my buggy code, I’m on my phone and firefox can be a pain with letter cases sometimes.

I’d be happy to elaborate if I havent been clear.

Thanks in advance

Hi. Thanks for your question. We’ll follow up here as soon as possible.

Hi,
The question is rather broad one :wink:
A few people tried things that seem similar to what you want:

A helpful start would be cling’s developers’ documentation: cling.web.cern.ch/cling/doxygen/annotated.html

If that is not helpful enough, could you be more specific.
Cheers,
Vassil

Hey guys, thanks for responding

Ill try to elaborate with a more verbose code example.

#include <iostream>
#include <vector>

struct copyable
{
double real;
std::vector <int>vec;
getNum ( ) {return num_; }
setNum (const int & num ){num_ = num; }
private: 
int num_;
}

In main () 
{
copyable Foo;

Foo.real = 6.789;
Foo.vec.resize (6);
Foo.vec [0]  = 5;
Foo.vec [1] = 7;
Foo.vec [2] = 9;
Foo.setNum (99);
/* from here it should be clear what the expected values are if they were printed to stdout at this point*/

cling::Interpreter interpreter;
Interpreter.bind (Foo, "FooRef");

interpreter.process (
"#include <vector>\n"
"FooRef.vec [2] = 4;\n"
"FooRef.vec [3] = 55;\n"
"FooRef.real = 7.789;\n"
"FooRef.setNum (234);\n"

);
/* from here i would expect Foo.num to be 234, Foo.real to be 7.789, vec to be {5,7,4,55,uninit,uinit}*/
return 0;
}

When i was looking at the cling interpreter header, i saw no straightfoward way of embedding cling that can get access to its host’s memory.

I hope this is more clear

Thanks

Hi,
The suggested links seem exactly what you want. The source code of “qling” even up on github github.com/cptG/qling
This project successfully embeds the cling interpreter.
Cheers,
Vassil

PS: If you want to access compiled code you should put it in a library, load the library (.L libraryName.so) and include the relevant header files. Thus you should be able to interact with compiled code. cling::Interpreter has interfaces for doing all this.

So I havent been able to get it to work yet, but it seems that I need to make use of the MetaProcessor class. A problem im dealing with after running make install (on a special prefix), I can’t seem to find MetaProcessor.hpp anywhere in the installed prefix even though it happily seems to reside in the original source.
Right now I’m trying to get it to work by including the directory from the original source, but that doesn’t work unless i selectively copy certain source files from the main source to the prefix’d etc folder

a cmake snippet of how im including it is
include_directories(${CLING_PREFIX}/etc/) # this contains the [limited] cling and llvm directories

Thanks,
d4nf

p.s. I’m using the latest tarball (6.04.06)

Hi,

cling is not ROOT.

When you use ROOT, cling is a hidden ingredient. It is not part of ROOT’s interfaces.

If you build cling standalone, using the build procedure from https://root.cern.ch/cling-build-instructions, you will get MetaProcessor.h installed.

Cheers, Axel.