Skip to content

Instantly share code, notes, and snippets.

@MadFaill
Last active October 21, 2015 11:33
Show Gist options
  • Save MadFaill/9009bd97a8d109cf94b0 to your computer and use it in GitHub Desktop.
Save MadFaill/9009bd97a8d109cf94b0 to your computer and use it in GitHub Desktop.
python ext example

python ext example

  • sudo apt-get update
  • sudo apt-get install python-dev -y
  • python setup.py install
  • python foo_test.py
#include <Python.h>
static PyObject *foo_add(PyObject *self, PyObject *args)
{
int a;
int b;
if (!PyArg_ParseTuple(args, "ii", &a, &b))
{
return NULL;
}
return Py_BuildValue("i", a + b);
}
static PyMethodDef foo_methods[] = {
{ "add", (PyCFunction)foo_add, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initfoo()
{
Py_InitModule3("foo", foo_methods, "My first extension module.");
}
import foo
print foo.add(1, 1)
from distutils.core import setup, Extension
module1 = Extension('foo', sources=['foo.c'])
setup(name='foo',
version='1.0',
description='This is a demo package',
ext_modules=[module1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment