Python和C之间的通讯++ [英] communicate between python and C++

查看:143
本文介绍了Python和C之间的通讯++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Python模块,可以从C ++类称为它的功能和从该类调用C ++函数

I want to create a python module which can have its functions called from a C++ class and call c++ functions from that class

我已经看过提升但它似乎并没有任何意义
它指的是一个共享库(我不知道如何建立),我不能休耕他们示例使用code(似乎很混乱)

i have looked at boost however it hasn't seemed to make any sense it refers to a shared library (which i have no idea how to create) and i cant fallow the code they use in examples (it seems very confusing)

这里是他们的hello world教程
(<一href=\"http://www.boost.org/doc/libs/1_55_0b1/libs/python/doc/tutorial/doc/html/index.html#python.quickstart\" rel=\"nofollow\">http://www.boost.org/doc/libs/1_55_0b1/libs/python/doc/tutorial/doc/html/index.html#python.quickstart)

here is their hello world tutorial (http://www.boost.org/doc/libs/1_55_0b1/libs/python/doc/tutorial/doc/html/index.html#python.quickstart)

以下的C / C ++的传统,让我们开始用你好,世界。 C ++函数:

Following C/C++ tradition, let's start with the "hello, world". A C++ Function:

char const* greet()
{
   return "hello, world";
}

可以通过编写Boost.Python的包装暴露的Python:

can be exposed to Python by writing a Boost.Python wrapper:

include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

就是这样。我们就大功告成了。现在,我们可以建立这个作为共享库。由此产生的DLL是现在
可见到Python。下面是一个示例Python会话:

That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python. Here's a sample Python session:

>>> import hello_ext
>>> print hello_ext.greet()
hello, world

下一站......从开始建立你的Hello World模块来完成...

Next stop... Building your Hello World module from start to finish...

可能有人请帮忙解释正在做什么,最重要的蟒蛇怎么知道的C ++文件

could someone please help explain what is being done and most of all how python knows about the C++ file

推荐答案

Python不知道C ++的文件,也只会是意识到的扩展模块的是从C ++文件编译。这个扩展模块是一个对象文件,称为共享库。这个文件看起来对Python的,就好像它是一个正常的Python模块的接口。

Python does not know about the C++ file, it will only be aware of the extension module that is compiled from the C++ file. This extension module is an object file, called a shared library. This file has an interface that looks to Python as if it was a normal Python module.

您告诉编译器来编译C ++文件,并与所有需要的库链接后这个目标文件才会存在。当然,首先需要的库Boost.Python的本身,它必须是可用的你在哪里编译系统上。

This object file will only exist after you tell a compiler to compile the C++ file and link it with all the libraries it needs. Of course, the first library needed is Boost.Python itself, which must be available on the system where you are compiling.

您可以告诉Python编译C ++文件给你,让你不必乱用编译器和它的图书馆标志。为了做到这一点,你需要一个名为 setup.py ,其中使用的setuptools库或Distutils的标准来定义你的其他Python模块是如何被上安装系统。其中一个用于安装步骤正在编制所有的扩展模块,名为 build_ext 阶段。

You can tell Python to compile the C++ file for you, so that you do not need to mess with the compiler and its library flags. In order to do so, you need a file called setup.py where you use the Setuptools library or the standard Distutils to define how your other Python modules are to be installed on the system. One of the steps for installing is compiling all extension modules, called the build_ext phase.

让我们设想你有以下目录和文件:

Let us imagine you have the following directories and files:

hello-world/
├── hello_ext.cpp
└── setup.py

setup.py 的内容是:

from distutils.core import setup
from distutils.extension import Extension


hello_ext = Extension(
    'hello_ext',
    sources=['hello_ext.cpp'],
    include_dirs=['/opt/local/include'],
    libraries=['boost_python-mt'],
    library_dirs=['/opt/local/lib'])


setup(
    name='hello-world',
    version='0.1',
    ext_modules=[hello_ext])

正如你所看到的,我们告诉Python中有我们希望编译源代码文件是一个扩展,其中包括库被发现。的这是系统相关的的。这里显示的例子是一个Mac OS X系统,其中Boost库通过了安装MacPorts的

As you can see, we are telling Python there is an Extension we want to compile, where the source file is, and where the included libraries are to be found. This is system-dependent. The example shown here is for a Mac OS X system, where Boost libraries were installed via MacPorts.

hello_ext.cpp 的内容是如图所示的教程,但是要注意的事重新排序,从而使 BOOST_PYTHON_MODULE 巨集出现任何的必须出口到Python的定义后的

The content of hello_ext.cpp is as shown in the tutorial, but take care to reorder things so that the BOOST_PYTHON_MODULE macro comes after the definitions of whatever must be exported to Python:

#include <boost/python.hpp>

char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

您可以再告诉Python来编译和链接你通过执行命令行中执行以下操作:

You can then tell Python to compile and link for you by executing the following on the command line:

$ python setup.py build_ext --inplace
running build_ext
building 'hello_ext' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -pipe -Os -fwrapv -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/opt/local/include -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c hello_ext.cpp -o build/temp.macosx-10.9-x86_64-2.7/hello_ext.o
/usr/bin/clang++ -bundle -undefined dynamic_lookup -L/opt/local/lib -Wl,-headerpad_max_install_names -L/opt/local/lib/db46 build/temp.macosx-10.9-x86_64-2.7/hello_ext.o -L/opt/local/lib -lboost_python-mt -o ./hello_ext.so

(即 - 就地标记告诉Python的离开编译的产品旁边的源文件,默认是将其移动到构建目录,以保持源目录干净。)

(The --inplace flag tells Python to leave the products of compilation right next to the source files. The default is to move them to a build directory, to keep the source directory clean.)

在这之后,你会发现一个名为新文件 hello_ext.dll (或 hello_ext.so Unix上)在的hello-world 目录。如果你开始在该目录中一个Python间preTER,你就能导入模块 hello_ext 和使用功能问候,如图所示的升压教程。

After that, you will find a new file called hello_ext.dll (or hello_ext.so on Unix) on the hello-world directory. If you start a Python interpreter in that directory, you will be able to import the module hello_ext and use the function greet, as shown in the Boost tutorial.

这篇关于Python和C之间的通讯++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆