你好ç:
#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:
#!/usr/bin/env pythonfrom setuptools import setup,Extensionsetup( name='helloworld',version='1.0',ext_modules=[ Extension('helloworld',['hello.c']) ])
建立:
$python setup.py developrunning developrunning egg_infocreating helloworld.egg-infowriting helloworld.egg-info/PKG-INFOwriting top-level names to helloworld.egg-info/top_level.txtwriting dependency_links to helloworld.egg-info/dependency_links.txtwriting 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_extbuilding 'helloworld' extensioncreating buildcreating build/temp.linux-x86_64-2.7x86_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.ocreating build/lib.linux-x86_64-2.7x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -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.socopying 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 fileInstalled /home/prologic/tmp/hello-py-cProcessing dependencIEs for helloworld==1.0Finished processing dependencIEs for helloworld==1.0
测试:
$pythonPython 2.7.6 (default,Mar 22 2014,22:59:56) [GCC 4.8.2] on linux2Type "help","copyright","credits" or "license" for more information.>>> import helloworld>>> helloworld.helloworld()'Hello,Python extensions!!'>>>
祝好运!
总结以上是内存溢出为你收集整理的有关从C创建Python库的建议吗?全部内容,希望文章能够帮你解决有关从C创建Python库的建议吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)