gpt4 book ai didi

c++ - C/C++ Python 解释器

转载 作者:太空狗 更新时间:2023-10-29 23:37:08 25 4
gpt4 key购买 nike

我想做的是用 C/C++ 编写一个应用程序,它允许用户输入一个 Python 脚本,然后该应用程序将解释并运行该脚本。上述脚本将有一个单独的 API,我将用 C 语言实现该 API,然后向用户公开。

有没有办法做到这一点?我在 Google 上搜索过,但我只找到了创建“扩展”的方法,而不是实际的解释器(如 Lua 允许的那样)。

最佳答案

关于 Embedding Python in Another Application 的文档说:

The previous chapters discussed how to extend Python, that is, how to extend the functionality of Python by attaching a library of C functions to it. It is also possible to do it the other way around: enrich your C/C++ application by embedding Python in it. Embedding provides your application with the ability to implement some of the functionality of your application in Python rather than C or C++. This can be used for many purposes; one example would be to allow users to tailor the application to their needs by writing some scripts in Python. You can also use it yourself if some of the functionality can be written in Python more easily.

特别关注Extending Embedded Python :

Until now, the embedded Python interpreter had no access to functionality from the application itself. The Python API allows this by extending the embedded interpreter. That is, the embedded interpreter gets extended with routines provided by the application.

只需阅读我引用的文档,您应该能够在用 C 编写的应用程序中实现 Python 解释器。

关于c++ - C/C++ Python 解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com