如何通过API在我的应用中使用Cling来解释C++代码?
我希望它能提供类似终端的交互方式,而不需要编译/运行可执行文件。假设我有hello world程序:
void main() {
cout << "Hello world!" << endl;
}我希望有应用程序接口来执行char* = (program code)和获取char *output = "Hello world!"。谢谢。
PS。类似于ch interpeter example的内容
/* File: embedch.c */
#include <stdio.h>
#include <embedch.h>
char *code = "\
int func(double x, int *a) { \
printf(\"x = %f\\n\", x); \
printf(\"a[1] in func=%d\\n\", a[1]);\
a[1] = 20; \
return 30; \
}";
int main () {
ChInterp_t interp;
double x = 10;
int a[] = {1, 2, 3, 4, 5}, retval;
Ch_Initialize(&interp, NULL);
Ch_AppendRunScript(interp,code);
Ch_CallFuncByName(interp, "func", &retval, x, a);
printf("a[1] in main=%d\n", a[1]);
printf("retval = %d\n", retval);
Ch_End(interp);
}
}发布于 2017-08-15 01:18:25
最后有一个更好的答案:示例代码!请参阅https://github.com/root-project/cling/blob/master/tools/demo/cling-demo.cpp
你的问题的答案是:不。cling接受代码并返回C++值或对象,跨越编译和解释的代码。这不是一种“字符串输入/字符串输出”的东西。perl可以做到这一点;-)这是代码输入,值输出的样子:
// We could use a header, too...
interp.declare("int aGlobal;\n");
cling::Value res; // Will hold the result of the expression evaluation.
interp.process("aGlobal;", &res);
std::cout << "aGlobal is " << res.getAs<long long>() << '\n';很抱歉回复得太晚了!
发布于 2014-08-03 21:57:47
通常的做法是:[cling$] #include "cling/Interpreter/Interpreter.h" [cling$] const char* someCode = "int i = 123;" [cling$] gCling->declare(someCode); [cling$] i // You will have i declared: (int) 123
该接口的文档为:http://cling.web.cern.ch/cling/doxygen/classcling_1_1Interpreter.html
当然,你也可以在cling的运行时创建自己的“嵌套”解释器。(参见上面的doxygen链接)
我希望它能帮助你回答这个问题,你可以在test/文件夹中找到更多的用法示例。Vassil
https://stackoverflow.com/questions/24077236
复制相似问题