vs实现python c扩展模块
生活随笔
收集整理的這篇文章主要介紹了
vs实现python c扩展模块
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
官方示例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:添加包含頭文件以及動(dòng)態(tài)鏈接庫(kù)目錄
Debug模式下編譯提示:
1>LINK : fatal error LNK1104: 無(wú)法打開(kāi)文件“python27_d.lib”pyconfig.h 文件部分內(nèi)容如下:
# ifdef _DEBUG # pragma comment(lib,"python27_d.lib") # else # pragma comment(lib,"python27.lib") # endif /* _DEBUG */默認(rèn)python27_d.lib不存在,所以編譯時(shí)候選擇Release模式,編譯成功后將目錄下spam.dll改為python識(shí)別的pyd后綴。
測(cè)試test.py:
import spamprint dir(spam) a = spam.system('echo hello ')?
轉(zhuǎn)載于:https://www.cnblogs.com/persuit/p/6379140.html
總結(jié)
以上是生活随笔為你收集整理的vs实现python c扩展模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: angularJs项目实战!04:ang
- 下一篇: Ybquery项目部署idea