简体   繁体   中英

C/C++ Python interpreter

What I'm trying to do is write an app in C/C++ which will allow users to enter a Python script, which the app will then interpret & run. The aforementioned script will have a separate API that I'll implement in C, then expose to the user.

Is there a way to do this? I've searched on Google, but I've only found ways to create 'extensions' and not actual interpreters (like Lua allows).

Documentation about Embedding Python in Another Application says:

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.

Look especially into 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 .

Just read the docs I referenced and you should be able to implement Python interpreter within your app written in C.

您可以使用Boost Python进行从C ++到Python的调用,反之亦然

If you're interested I recently implemented embedded Python scripting for C++ from first principles. I created a C++ Python wrapper library called ECS:Python (Embedded C++ Scripting with Python) that allows you to expose object from a C++ application to an embedded Python interpreter for interactive scripting. It's light-weight and very easy to use (and free!).

http://sourceforge.net/projects/ecspython

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM