欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python調用C

系統 1650 0

文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡書

1. 引言

眾所周知,Python語言簡單、易學、開源、具有豐富的庫,Python的第一個編譯器是用C語言實現的。但Python的缺點也非常明顯,最讓人詬病的就是Python的性能問題。因此,為了提高程序的運行效率,通常會將程序的關鍵部分使用C或C++重寫,編譯成動態鏈接庫,然后在Python(CPython)中進行調用。運行環境:Ubuntu 16.04、Python 2.7、Python 3.5。

2. Python C擴展

2.1 普通C函數

            
              void hello()
{
	printf("Hello World!\n");
}

int add(int a, int b)
{
	return a + b;
}

            
          

2.2 Python C擴展

Python擴展模塊由以下幾部分組成:

  • 頭文件
  • 調用的C函數
  • 模塊方法表
  • 模塊初始化函數

具體實現 demo.c 如下:

            
              // 包含Python頭文件
#include 
              
                


// 兼容Python3
#if PY_MAJOR_VERSION >= 3
#define PYTHON3
#endif


// hello函數實現
static PyObject* hello(PyObject *self, PyObject *args)
{
    printf("Hello World\n");
    return Py_None;
}


// add函數實現
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模塊定義結構體
static struct PyModuleDef testModule = {
	PyModuleDef_HEAD_INIT,
	"testModule",
	"Test Module",
	-1,
	TwoMethods
};


// Python3模塊初始化函數
PyMODINIT_FUNC PyInit_demo(void)
{
	return PyModule_Create(&testModule);
}


#else
// Python2模塊初始化函數
PyMODINIT_FUNC initdemo(void)
{
    Py_InitModule("demo", TwoMethods);
}
#endif

              
            
          

2.3 編譯并測試

編寫 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])

            
          

生成動態鏈接庫的命令如下:

            
              #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 函數測試:

            
              >>> from demo import hello, add
>>> hello()
Hello World
>>> add(2, 3)
5

            
          

參考資料

  1. https://www.cnblogs.com/vamei/archive/2013/02/06/2892628.html
  2. https://www.yanxurui.cc/posts/python/2017-06-18-3-ways-of-calling-c-functions-from-python/
  3. https://swe.mirsking.com/languages/python/pythoncallcplusplus
  4. https://www.jianshu.com/p/cd28e8b0cce1
  5. https://docs.python.org/2.7/extending/extending.html
  6. https://docs.python.org/2.7/extending/building.html
  7. https://tutorialedge.net/python/python-c-extensions-tutorial/

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久99热久久精品23 | 特级做a爰片毛片免费看一区 | 欧美1024性视频 | 亚洲国产精品久久综合 | 色在线视频 | 色爱av| 亚洲精品国产福利在线观看 | 狠狠色噜噜狠狠狠狠米奇7777 | 色聚网久久综合 | 国产精品久久久久久久y | 亚洲成人日韩 | 久久久影院 | 久久这里只有精品99 | 不卡久久| 欧美日韩国产成人在线 | 国产亚洲精品精品国产亚洲综合 | 亚洲日本一区二区三区 | 少妇特黄A片一区二区三区免费看 | 日韩成人高清 | 在线手机电影 | 日日摸夜夜添夜夜添精品视频 | 久久一er精这里有精品 | 精品国产不卡一区二区三区 | 亚洲综合99 | 日韩国产一区 | 日本黄色视 | 欧美视频成人 | 奇米影视在线观看 | 亚洲www啪成人一区二区麻豆 | 欧美亚洲国产一区 | 精品久久久久久久久久久 | 久热中文字幕在线 | 欧美14一15sex性hd | av在线一区二区三区 | 日韩三区 | 亚洲欧美视频一区 | 亚洲w码 | 午夜影皖 | 欧美黄视频网站 | 清清草免费视频 | 激情亚洲|