extends python2.7 with cpp by visual studio 2017

1

1- project -> properties

2- All Configurations -> x64

3- General
Target Extension: .pyd

4- VC++ Directories
Include Directories: D:\Python27\include
Library Directories: D:\Python27\libs

2

1- include.h

#ifdef _DEBUG
#define _DEBUG_WAS_DEFINED 1
#undef _DEBUG
#endif // _DEBUG

#include <Python.h>

#ifdef _DEBUG_WAS_DEFINED
#define _DEBUG 1
#endif // _DEBUG_WAS_DEFINED


2- main.cpp

#include "include.h"

PyObject* some_function(PyObject* self, PyObject* args)
{
	int i;
	if (!PyArg_ParseTuple(args, "i", &i)) {
		goto error;
	}
	return PyInt_FromLong(i + 1);
error:
	return 0;
}

PyMethodDef SpamMethods[] =
{
	{"add_one", (PyCFunction)some_function, METH_VARARGS, 0},
	{0, 0, 0, 0}
};

PyMODINIT_FUNC
initspam(void)
{
	PyObject *m;

	m = Py_InitModule("spam", SpamMethods);
	if (m == NULL)
		return;
}

posted @ 2017-06-13 09:53  idlewith  阅读(160)  评论(0编辑  收藏  举报