For the C extension of 32-bit Python, it was previously compiled with MINGW32,
But MingW32 does not support the 64-bit Python C extension compilation, details can be seen StackOverflow, the predecessor of the main idea is as follows,
The following describes the C extension steps for Visual Studio 2013 compiling 64-bit Python:
1) Prepare C files and packaging documents,
Extdemo.c
// Purpose: C code, for wrappered.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fact(int n)
{
if(n < 2)
return 1;
return n * fact(n - 1);
}
char * reverse(char * s)
{
char t;
char *p = s;
char *q = (s + (strlen(s) - 1));
while(p < q)
{
t = *p;
*p++ = *q;
*q-- = t;
}
return s;
}
// just for unit test, for the two function above
int unit_test(void)
{
// test fact()
printf("4! = %d\n", fact(4));
printf("8! = %d\n", fact(8));
printf("12! = %d\n", fact(12));
// test reverse
char s[10] = "abcdef";
printf("reversing ‘abcdef‘, we get ‘%s‘\n", reverse(s));
char s2[10] = "madam";
printf("reversing ‘madam‘, we get ‘%s‘\n", reverse(s2));
return 0;
}
Packing Code EXTDEMO_WRAPPER.C
// Purpose: According to the C code, write the Wrapper.
#include "Python.h"
// function declaration
int fact(int n);
char * reverse(char * s);
int unit_test(void);
static PyObject * ED_fact(PyObject * self, PyObject * args)
{
int num;
if(!PyArg_ParseTuple(args, "i", &num))
return NULL;
return (PyObject *)Py_BuildValue("i", fact(num));
}
static PyObject * ED_reverse(PyObject * self, PyObject * args)
{
char * orig_str;
if (!PyArg_ParseTuple(args, "s", &orig_str))
return NULL;
return (PyObject *)Py_BuildValue("s", reverse(orig_str));
}
static PyObject * ED_unit_test(PyObject * self, PyObject * args)
{
unit_test();
return (PyObject *)Py_BuildValue("");
}
//////////////////////////////////////////////////////////////////////////////
static PyMethodDef EDMethods[] = {
{"fact", ED_fact, METH_VARARGS},
{"reverse", ED_reverse, METH_VARARGS},
{"unit_test", ED_unit_test, METH_VARARGS},
{NULL, NULL},
};
//////////////////////////////////////////////////////////////////////////////
void initED()
{
Py_InitModule("ED", EDMethods);
}
setup.py
#!/usr/bin/env python
from distutils.core import setup, Extension
MOD = ‘ED‘
setup(name=MOD, ext_modules=[
Extension(MOD, sources=[‘ExtDemo.c‘, "ExtDemo_Wrapper.c"])])
2) Visual Studio 2013 Tools Preparation and compilation
The Start menu opens the Visual Studio Tools folder,
Select 64bit Native Tools, double-click Open,
Set the compilation environment, as below, refer to the Distutils.core official help document for the meanings of these two parameters.
Set distutils_use_sdk=1set MSSDK=1
Switch to the project catalog, compile,
After the compilation is complete, build folder is generated under the project directory,
In which \build\lib.win-amd64-2.7 to get compiled generated PYD file, this example is Ed.pyd
3) Verification
Finish.
Visual Studio 2013 compiles C extensions for 64-bit Python (using Pyobject wrapper)