将 C++ 函数导入 Python 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4241415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:54:34  来源:igfitidea点击:

Import C++ function into Python program

c++pythonfunctionimport

提问by Nick

I'm experimenting with python functions right now. I've found a way to import python functions into c/c++ code, but not the other way around.

我现在正在试验 python 函数。我找到了一种将 python 函数导入 c/c++ 代码的方法,但反过来不行。

I have a c++ program written and it has a certain function in it. I'd like to "import" the compiled c++ program into my python script and call the c++ function.

我写了一个 C++ 程序,它有一个特定的功能。我想将编译后的 c++ 程序“导入”到我的 python 脚本中并调用 c++ 函数。

For simplicity, say the c++ function is as simple as:

为简单起见,假设 c++ 函数很简单:

int square(x)
{
  return x*x;
}

and the compiled program is named Cprog.

编译后的程序名为Cprog。

I'd like my python script to be something like:

我希望我的 python 脚本类似于:

import Cprog

print Cprog.square(4)

Is this possible? I've searched the internet to no avail and I'm hoping one of you gurus might have a clever way of going about this...

这可能吗?我在互联网上搜索无果,我希望你们中的一位大师可能有一个聪明的方法来解决这个问题......

回答by kichik

If you build your program as a shared library/DLL, you could use ctypes to call it.

如果您将程序构建为共享库/DLL,则可以使用 ctypes 来调用它。

import ctypes
print ctypes.windll.cprog.square(4) # windows
print ctypes.CDLL('cprog.so').square(4) # linux

回答by Anthony

You want to extendpython with a C/C++ module. The following Python documentation is a good place to start reading: http://docs.python.org/extending/extending.html

你想用 C/C++ 模块扩展python。以下 Python 文档是开始阅读的好地方:http: //docs.python.org/extending/extending.html

回答by DaedalusFall

You need to create a python module with that function in it. There are three main ways:

您需要创建一个包含该函数的 python 模块。主要有以下三种方式:

  1. Using Swig - this reads your c code and creates a python module from it.
  2. Hand coded using the python c api.
  3. Using Boost::Python (often the easiest way).
  1. 使用 Swig - 这会读取您的 c 代码并从中创建一个 python 模块。
  2. 使用 python c api 手工编码。
  3. 使用 Boost::Python(通常是最简单的方法)。

This pdfcovers 1 and 2. This pagewill tell you how to use Boost::Python.

该 pdf涵盖 1 和 2。此页面将告诉您如何使用 Boost::Python。

You cannot (easily) use a function that is in a c/c++ program - it must be in a static library (which you can also link your c/c++ program against).

您不能(轻松)使用 ac/c++ 程序中的函数 - 它必须位于静态库中(您也可以将您的 c/c++ 程序链接到该库中)。

EDIT - CythonIs also worth a mention.

编辑 - Cython也值得一提。

回答by Jonathan Sternberg

There are a lot of different ways to wrap C++ code to be used in Python. Most are listed on the Python wiki here.

有很多不同的方法来包装要在 Python 中使用的 C++ 代码。大多数都列在此处的 Python wiki 上。

I've found a decently easy way to automate it is to use py++to automatically generate the wrappers, then compile the generated files. But this is more appropriate for larger projects where wrapping everything by hand is just not very feasible and would cause a maintenence nightmare.

我发现一种相当简单的自动化方法是使用py++自动生成包装器,然后编译生成的文件。但这更适合大型项目,因为手动包装所有东西不太可行,并且会导致维护噩梦。

回答by dolby

Here is a little working completion of the simple example above. Although the thread is old, I think it is helpful to have a simple all-embracing guide for beginners, because I also had some problems before.

这是上面简单示例的一些工作完成。虽然线程老了,但是我觉得给初学者一个简单的包罗万象的指南还是有帮助的,因为我之前也遇到过一些问题。

function.cpp content (extern "C" used so that ctypes module can handle the function):

function.cpp 内容(使用 extern "C" 以便 ctypes 模块可以处理该功能):

extern "C" int square(int x)
{
  return x*x;
}

wrapper.py content:

wrapper.py 内容:

import ctypes
print(ctypes.windll.library.square(4)) # windows
print(ctypes.CDLL('./library.so').square(4)) # linux or when mingw used on windows

Then compile the function.cpp file (by using mingw for example):

然后编译 function.cpp 文件(例如使用 mingw):

g++ -shared -c -fPIC function.cpp -o function.o

Then create the shared object library with the following command (note: not everywhere are blanks):

然后使用以下命令创建共享对象库(注意:并非处处都是空白):

g++ -shared -Wl,-soname,library.so -o library.so function.o

Then run the wrapper.py an the program should work.

然后运行 ​​wrapper.py 程序应该可以工作。