文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡(jiǎn)書(shū)
1. 引言
眾所周知,Python語(yǔ)言簡(jiǎn)單、易學(xué)、開(kāi)源、具有豐富的庫(kù),Python的第一個(gè)編譯器是用C語(yǔ)言實(shí)現(xiàn)的。但Python的缺點(diǎn)也非常明顯,最讓人詬病的就是Python的性能問(wèn)題。因此,為了提高程序的運(yùn)行效率,通常會(huì)將程序的關(guān)鍵部分使用C或C++重寫(xiě),編譯成動(dòng)態(tài)鏈接庫(kù),然后在Python(CPython)中進(jìn)行調(diào)用。運(yùn)行環(huán)境:Ubuntu 16.04、Python 2.7、Python 3.5。
2. Python C擴(kuò)展
2.1 普通C函數(shù)
void hello()
{
printf("Hello World!\n");
}
int add(int a, int b)
{
return a + b;
}
2.2 Python C擴(kuò)展
Python擴(kuò)展模塊由以下幾部分組成:
-
頭文件
- 調(diào)用的C函數(shù)
- 模塊方法表
- 模塊初始化函數(shù)
具體實(shí)現(xiàn)
demo.c
如下:
// 包含Python頭文件
#include
// 兼容Python3
#if PY_MAJOR_VERSION >= 3
#define PYTHON3
#endif
// hello函數(shù)實(shí)現(xiàn)
static PyObject* hello(PyObject *self, PyObject *args)
{
printf("Hello World\n");
return Py_None;
}
// add函數(shù)實(shí)現(xiàn)
static PyObject* add(PyObject *self, PyObject *args)
{
int a, b;
if(!PyArg_ParseTuple(args, "ii", &a, &b))
{
return NULL;
}
return Py_BuildValue("i", a + b);
}
// 模塊方法表
static PyMethodDef TwoMethods[] = {
{ "hello", hello, METH_NOARGS, "Print Hello" },
{ "add", add, METH_VARARGS, "Add two integers"},
{ NULL, NULL, 0, NULL }
};
#ifdef PYTHON3
// Python3模塊定義結(jié)構(gòu)體
static struct PyModuleDef testModule = {
PyModuleDef_HEAD_INIT,
"testModule",
"Test Module",
-1,
TwoMethods
};
// Python3模塊初始化函數(shù)
PyMODINIT_FUNC PyInit_demo(void)
{
return PyModule_Create(&testModule);
}
#else
// Python2模塊初始化函數(shù)
PyMODINIT_FUNC initdemo(void)
{
Py_InitModule("demo", TwoMethods);
}
#endif
2.3 編譯并測(cè)試
編寫(xiě)
setup.py
文件:
from distutils.core import setup, Extension
demo = Extension('demo', sources = ['demo.c'])
setup(name = 'C extension module', version = '1.0', description = 'This is a demo', ext_modules = [demo])
生成動(dòng)態(tài)鏈接庫(kù)的命令如下:
#python2
$ python setup.py build_ext --inplace
running build_ext
building 'demo' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c demo.c -o build/temp.linux-x86_64-2.7/demo.o
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 -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/demo.o -o /workspace/python-c/demo.so
#python3
$ python3 setup.py build_ext --inplace
running build_ext
building 'demo' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c demo.c -o build/temp.linux-x86_64-3.5/demo.o
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/demo.o -o /workspace/python-c/demo.cpython-35m-x86_64-linux-gnu.so
hello
,
add
函數(shù)測(cè)試:
>>> from demo import hello, add
>>> hello()
Hello World
>>> add(2, 3)
5
參考資料
- https://www.cnblogs.com/vamei/archive/2013/02/06/2892628.html
- https://www.yanxurui.cc/posts/python/2017-06-18-3-ways-of-calling-c-functions-from-python/
- https://swe.mirsking.com/languages/python/pythoncallcplusplus
- https://www.jianshu.com/p/cd28e8b0cce1
- https://docs.python.org/2.7/extending/extending.html
- https://docs.python.org/2.7/extending/building.html
- https://tutorialedge.net/python/python-c-extensions-tutorial/
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
