有关将 Clang 作为脚本解释器嵌入到 C++ 中的任何教程;代码?

发布于 2024-09-09 04:22:06 字数 269 浏览 5 评论 0原文

我还没有 llvm 或 clang 的经验。据我所知,clang 据说很容易嵌入 Wikipedia-Clang,但是,我没有找到有关如何实现此目标的任何教程。那么是否可以通过在运行时 JIT 编译和执行用户定义的代码来为 C++ 应用程序的用户提供脚本编写能力呢?是否可以调用应用程序自己的类和方法并共享对象?

编辑:我更喜欢脚本语言的类似 C 的语法(甚至 C++ 本身)

I have no experience with llvm or clang, yet. From what I read clang is said to be easily embeddable Wikipedia-Clang, however, I did not find any tutorials about how to achieve this. So is it possible to provide the user of a c++ application with scripting-powers by JIT compiling and executing user-defined code at runtime? Would it be possible to call the applications own classes and methods and share objects?

edit: I'd prefer a C-like syntax for the script-languge (or even C++ itself)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浪菊怪哟 2024-09-16 04:22:06

我不知道任何教程,但 Clang 源代码中有一个示例 C 解释器可能会有所帮助。您可以在这里找到它: http://llvm.org/ viewvc/llvm-project/cfe/trunk/examples/clang-interpreter/

如果您选择这条路线,您的脚本语言语法可能没有太多选择。 Clang 仅解析 C、C++ 和 Objective C。如果您想要任何变化,您可能已经完成了您的工作。

I don't know of any tutorial, but there is an example C interpreter in the Clang source that might be helpful. You can find it here: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/clang-interpreter/

You probably won't have much of a choice of syntax for your scripting language if you go this route. Clang only parses C, C++, and Objective C. If you want any variations, you may have your work cut out for you.

韶华倾负 2024-09-16 04:22:06

我想这就是你所描述的。

http://root.cern.ch/drupal/content/cling

I think here's what exactly you described.

http://root.cern.ch/drupal/content/cling

想挽留 2024-09-16 04:22:06

您可以使用 clang 作为库来实现 JIT 编译,如其他答案所述。
然后,您必须加载已编译的模块(例如,.so 库)。

为了实现这一点,您可以使用标准 dlopen (unix) 或 LoadLibrary (windows) 来加载它,然后使用 dlsym (unix) 动态引用已编译的函数,例如名称已知的“脚本”类似 main() 的函数。请注意,对于 C++,您必须使用损坏的符号。
一个可移植的替代方案是 GNU 的 libltdl

作为替代方案,“脚本”可以通过实现模块初始化函数或放置一些静态代码(将立即调用 C++ 全局定义对象的构造函数)在加载时自动运行。

加载的模块可以直接调用主应用程序中的任何内容。当然,通过使用正确的主应用程序的头文件,符号在编译时是已知的。

如果您想轻松地将 C++“插件”添加到您的程序中,并且先验地了解组件接口(假设您的主应用程序在模块加载到内存中之前从其 .h 中知道加载类的名称和接口),那么在您动态加载库,该类可以像静态链接一样使用。请确保在 dlopen() 其模块之前不要尝试实例化类的对象。

使用静态代码也可以实现很好的自动插件注册机制。

You can use clang as a library to implement JIT compilation as stated by other answers.
Then, you have to load up the compiled module (say, an .so library).

In order to accomplish this, you can use standard dlopen (unix) or LoadLibrary (windows) to load it, then use dlsym (unix) to dynamically reference compiled functions, say a "script" main()-like function whose name is known. Note that for C++ you would have to use mangled symbols.
A portable alternative is e.g. GNU's libltdl.

As an alternative, the "script" may run automatically at load time by implementing module init functions or putting some static code (the constructor of a C++ globally defined object would be called immediately).

The loaded module can directly call anything in the main application. Of course symbols are known at compilation time by using the proper main app's header files.

If you want to easily add C++ "plugins" to your program, and know the component interface a priori (say your main application knows the name and interface of a loaded class from its .h before the module is loaded in memory), after you dynamically load the library the class is available to be used as if it was statically linked. Just be sure you do not try to instantiate a class' object before you dlopen() its module.

Using static code allows to implement nice automatic plugin registration mechanisms too.

絕版丫頭 2024-09-16 04:22:06

我不了解 Clang,但你可能想看看 Ch:

http://www.softintegration.com/< /a>

这被描述为可嵌入或独立的 c/c++ 解释器。有一篇 Dobbs 博士的文章,其中包含将其嵌入此处的示例:

http://www.dobbs.com/architecture-and-design/212201774" drdobbs.com/architecture-and-design/212201774

我除了使用它之外没有做过更多的事情,但它似乎是一个稳定且成熟的产品。它是商业的、闭源的,但“标准”版本被描述为免费供个人和商业使用。然而,从许可证来看,“商业”似乎只包括公司内部使用,而不是嵌入到随后销售或分发的产品中。 (我不是律师,因此显然应该咨询 SoftIntegration 以确定许可条款。)

I don't know about Clang but you might want to look at Ch:

http://www.softintegration.com/

This is described as an embeddable or stand-alone c/c++ interpreter. There is a Dr. Dobbs article with examples of embedding it here:

http://www.drdobbs.com/architecture-and-design/212201774

I haven't done more than play with it but it seems to be a stable and mature product. It's commercial, closed-source, but the "standard" version is described as free for both personal and commercial use. However, looking at the license it seems that "commercial" may only include internal company use, not embedding in a product that is then sold or distributed. (I'm not a lawyer, so clearly one should check with SoftIntegration to be certain of the license terms.)

青朷 2024-09-16 04:22:06

我不确定嵌入像 Clang 这样的 C 或 C++ 编译器对于您来说是一个好主意。因为“脚本”,即(在运行时!)输入的(C 或 C++)代码可以是任意的,因此能够使整个应用程序崩溃。您通常不希望错误的用户输入导致应用程序崩溃。

请务必阅读每一个C程序员应该了解 未定义的行为,因为它是相关的并且也适用于 C++(包括应用程序使用的任何“C++ 脚本”)。请注意,不幸的是,很多 UB 不会崩溃进程(例如缓冲区溢出可能会损坏一些完全不相关的数据)。

如果您想嵌入解释器,请选择为此目的而设计的东西,例如 GuileLua,并注意脚本中的错误不会导致整个应用程序崩溃。有关解释器嵌入的更详细讨论,请参阅此答案

I am not sure that embedding a C or C++ compiler like Clang is a good idea in your case. Because the "script", that is the (C or C++) code fed (at runtime!) can be arbitrary so be able to crash the entire application. You usually don't want faulty user input to be able to crash your application.

Be sure to read What every C programmer should know about undefined behavior because it is relevant and applies to C++ also (including any "C++ script" used by your application). Notice that, unfortunately, a lot of UB don't crash processes (for example a buffer overflow could corrupt some completely unrelated data).

If you want to embed an interpreter, choose something designed for that purpose, like Guile or Lua, and be careful that errors in the script don't crash the entire application. See this answer for a more detailed discussion of interpreter embedding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文