简体   繁体   中英

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. 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

saySomething("Hello")

As a .py file I want it to call the greet function and pass "Hello" as an argument.

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.

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.

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 . 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.

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. Even if you use these tools, you will frequently have to get down to Python C API anyway or at least understand it.

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. 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).

So, the only thing left for me was to implement it from first principles (Python/C API). Hense, "ECS:Python" was born. ECS:Python (Embedded C++ Scripting with Python) is a simple C++ Python wrapper library I designed specifically for C++ developers. 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.

Its free (BSD) and open source. If you're interested here it is: http://sourceforge.net/projects/ecspython

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

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