首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于从C++创建Python库的建议?

关于从C++创建Python库的建议?
EN

Stack Overflow用户
提问于 2015-05-06 11:02:27
回答 2查看 155关注 0票数 4

我最近使用OpenGL库在C++中创建了一个3D和2D的力布局图可视化工具(它使用了一些物理原理)。谁能给我一些介绍性的提示,让它成为一个有用的Python库(问题或注意事项,以及我可能会遇到的潜在陷阱)?

EN

回答 2

Stack Overflow用户

发布于 2015-05-07 08:32:21

如果我没理解错的话,您想知道如何编写在Python中使用的C扩展。下面是一个简单的简单示例,说明如何:

hello.c

代码语言:javascript
复制
#include <Python.h>

static PyObject* helloworld(PyObject* self)
{
    return Py_BuildValue("s", "Hello, Python extensions!!");
}

static char helloworld_docs[] =
    "helloworld( ): Any message you want to put here!!\n";

static PyMethodDef helloworld_funcs[] = {
    {"helloworld", (PyCFunction)helloworld, 
     METH_NOARGS, helloworld_docs},
    {NULL}
};

void inithelloworld(void)
{
    Py_InitModule3("helloworld", helloworld_funcs,
                   "Extension module example!");
}

setup.py

代码语言:javascript
复制
#!/usr/bin/env python

from setuptools import setup, Extension

setup(
    name='helloworld',
    version='1.0',
    ext_modules=[
        Extension('helloworld', ['hello.c'])
    ]
)

内部版本:

代码语言:javascript
复制
$ python setup.py develop
running develop
running egg_info
creating helloworld.egg-info
writing helloworld.egg-info/PKG-INFO
writing top-level names to helloworld.egg-info/top_level.txt
writing dependency_links to helloworld.egg-info/dependency_links.txt
writing manifest file 'helloworld.egg-info/SOURCES.txt'
reading manifest file 'helloworld.egg-info/SOURCES.txt'
writing manifest file 'helloworld.egg-info/SOURCES.txt'
running build_ext
building 'helloworld' extension
creating build
creating build/temp.linux-x86_64-2.7
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c hello.c -o build/temp.linux-x86_64-2.7/hello.o
creating build/lib.linux-x86_64-2.7
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/hello.o -o build/lib.linux-x86_64-2.7/helloworld.so
copying build/lib.linux-x86_64-2.7/helloworld.so -> 
Creating /home/prologic/.virtualenvs/hellopyc/lib/python2.7/site-packages/helloworld.egg-link (link to .)
Adding helloworld 1.0 to easy-install.pth file

Installed /home/prologic/tmp/hello-py-c
Processing dependencies for helloworld==1.0
Finished processing dependencies for helloworld==1.0

测试:

代码语言:javascript
复制
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import helloworld
>>> helloworld.helloworld()
'Hello, Python extensions!!'
>>> 

祝好运!

票数 1
EN

Stack Overflow用户

发布于 2015-05-07 09:41:20

虽然James Mills的回答很好,但我会考虑其他选择--使用Boost.Python库来编写python扩展。Here就是一个基本的例子。看起来Boost.Python没有被维护(最后一次更新- 2009),但它仍然是一个非常好的选择。现在我正在一个项目中使用它,我不得不说,在经历了一点困难的开始之后,现在它真的很有用,而且非常简单。对我来说最重要的是它处理引用计数(这在Python/C API中很痛苦),允许修改c++代码中的python对象,并允许您注册自定义数据类型的转换器。因此,与其写:

代码语言:javascript
复制
MyClass test = convertPythonObjectToMyClass(pythonObject);
MyOtherClass test2 = convertPythonObjectMyOtherClass(pythonObject);

您可以使用register your converters,然后使用以下命令:

代码语言:javascript
复制
MyClass test = boost::python::extract<MyClass>(pythonObject);
MyOtherClass test2 = boost::python::extract<MyOtherClass>(pythonObject);

当然,您也可以考虑其他选项-有关其他解决方案的更多信息,请参阅this问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30066707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档