vs实现python c扩展模块
生活随笔
收集整理的這篇文章主要介紹了
vs实现python c扩展模块
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
官方示例spam.cpp:
#include <Python.h> static PyObject * spam_system(PyObject *self, PyObject *args) {const char *command;int sts;if (!PyArg_ParseTuple(args, "s", &command))return NULL;sts = system(command);return Py_BuildValue("i", sts); }static PyMethodDef SpamMethods[] = {{"system", spam_system, METH_VARARGS,"Execute a shell command."},{NULL, NULL, 0, NULL} };PyMODINIT_FUNC initspam(void) {(void) Py_InitModule("spam", SpamMethods); }1:添加包含頭文件以及動態鏈接庫目錄
Debug模式下編譯提示:
1>LINK : fatal error LNK1104: 無法打開文件“python27_d.lib”pyconfig.h 文件部分內容如下:
# ifdef _DEBUG # pragma comment(lib,"python27_d.lib") # else # pragma comment(lib,"python27.lib") # endif /* _DEBUG */默認python27_d.lib不存在,所以編譯時候選擇Release模式,編譯成功后將目錄下spam.dll改為python識別的pyd后綴。
測試test.py:
import spamprint dir(spam) a = spam.system('echo hello ')?
轉載于:https://www.cnblogs.com/persuit/p/6379140.html
總結
以上是生活随笔為你收集整理的vs实现python c扩展模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: angularJs项目实战!04:ang
- 下一篇: python scrapy 抓取脚本之家