简体   繁体   English

通过Python脚本调用C ++函数

[英]Calling a C++ Functions through a Python Script

I have a scenario where I have some functions in C++ classes and I want to be able to call them using a python script. 我有一个场景,我在C ++类中有一些函数,我希望能够使用python脚本调用它们。 Let's say I have a function 假设我有一个功能

void greet(_msg);
    std::cout >> _msg >> std::endl;

I want to be able to call it trough a custom Python call and pass arguments to it, for example using 我希望能够通过自定义Python调用来调用它并将参数传递给它,例如使用

saySomething("Hello")

As a .py file I want it to call the greet function and pass "Hello" as an argument. 作为.py文件,我希望它调用greet函数并将“Hello”作为参数传递。

I know it's a subject that has been throughly discussed, and I've done a share of research on embedding python in C++, I've managed to read values from a python script using the standard Python/C API and run a function in Python from C++ and pass argument to it, but I can't seem to get my head around how to achieve this specific outcome. 我知道这是一个经过深入讨论的主题,我已经完成了在C ++中嵌入python的研究,我已经设法使用标准的Python / C API从python脚本读取值并在Python中运行一个函数来自C ++并将参数传递给它,但我似乎无法理解如何实现这一特定结果。

I've had a look at ctypes and various wrappin libraries such as boost:python or swig, but I can't seem to understand to which degree they could help me achieve want I want. 我已经看过ctypes和各种包装库,例如boost:python或swig,但我似乎无法理解他们可以帮助我实现我想要的程度。

Depending on which version of Python you are interested in, 2.x or 3.x, read through the Extending and Embedding the Python Interpreter chapter for 2.x or 3.x . 根据您有兴趣,x或3 x其中Python版本,通过扩展和集成的Python解释器阅读本章2.X3.X You are interested only in extending Python, so section the 1. Extending Python with C or C++ will provide you with complete explanation how to implement what you need in order to be able to call your functions implemented in C++ from Python script. 您只对扩展 Python感兴趣,因此1.使用C或C ++扩展Python将为您提供完整的解释,说明如何实现所需,以便能够从Python脚本调用C ++实现的函数。

Certainly, there are numerous libraries and generators which allow you to wrap C/C++ APIs for Python (eg Boost.Python or SWIG ), but your case sounds simple enough, that for the purpose of learning it is IMO better to get familiar with Python C API. 当然,有许多库和生成器允许你为Python包装C / C ++ API(例如Boost.PythonSWIG ),但你的案例听起来很简单,为了学习它,IMO更好地熟悉Python C API。 Even if you use these tools, you will frequently have to get down to Python C API anyway or at least understand it. 即使你使用这些工具,你也经常需要开始使用Python C API,或者至少要理解它。

I recently needed to do this very thing. 我最近需要做这件事。 Boost.Python does what we're looking for (and more) but personally (as much as I love Boost) I find it a little overkill to have to drag in half the Boost library to get one feature. Boost.Python做了我们正在寻找的(以及更多),但我个人(尽管我喜欢Boost)我觉得有必要拖延Boost库中的一半以获得一个功能。 SWIG also wasn't really an option for me as code generation always becomes a pain to maintain while class structures change (Don't get me wrong, these are BRILLIANT solutions!, just not what I was looking for). SWIG对我来说也不是一个真正的选择,因为在类结构发生变化时代码生成总是变得很难维护(不要误解我的意思,这些都是BRILLIANT解决方案!,而不是我想要的)。

So, the only thing left for me was to implement it from first principles (Python/C API). 所以,我唯一要做的就是从第一原则(Python / C API)实现它。 Hense, "ECS:Python" was born. Hense,“ECS:Python”诞生了。 ECS:Python (Embedded C++ Scripting with Python) is a simple C++ Python wrapper library I designed specifically for C++ developers. ECS:Python(嵌入式C ++脚本与Python)是一个简单的C ++ Python包装程序库,我专门为C ++开发人员设计。 It allows you to expose objects from a C++ application to an embedded Python interpreter for interactive scripting, and it's very light-weight and easy to use. 它允许您将对象从C ++应用程序公开到嵌入式Python解释器以进行交互式脚本编写,并且它非常轻巧且易于使用。

Its free (BSD) and open source. 它的免费(BSD)和开源。 If you're interested here it is: http://sourceforge.net/projects/ecspython 如果您对此感兴趣,请访问: http//sourceforge.net/projects/ecspython

您可以使用weave.inline()函数(它是scipy包的一部分)来编译和执行C / C ++文件,并从您的python脚本中获取它们的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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