首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cling API是否可用?

Cling API是否可用?
EN

Stack Overflow用户
提问于 2014-06-06 16:18:05
回答 2查看 562关注 0票数 4

如何通过API在我的应用中使用Cling来解释C++代码?

我希望它能提供类似终端的交互方式,而不需要编译/运行可执行文件。假设我有hello world程序:

代码语言:javascript
复制
void main() {
    cout << "Hello world!" << endl;
}

我希望有应用程序接口来执行char* = (program code)和获取char *output = "Hello world!"。谢谢。

PS。类似于ch interpeter example的内容

代码语言:javascript
复制
/* 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);
}
}
EN

回答 2

Stack Overflow用户

发布于 2017-08-15 01:18:25

最后有一个更好的答案:示例代码!请参阅https://github.com/root-project/cling/blob/master/tools/demo/cling-demo.cpp

你的问题的答案是:不。cling接受代码并返回C++值或对象,跨越编译和解释的代码。这不是一种“字符串输入/字符串输出”的东西。perl可以做到这一点;-)这是代码输入,值输出的样子:

代码语言:javascript
复制
// 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';

很抱歉回复得太晚了!

票数 4
EN

Stack Overflow用户

发布于 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24077236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档