- sudo apt-get update
- sudo apt-get install python-dev -y
- python setup.py install
- python foo_test.py
Last active
October 21, 2015 11:33
-
-
Save MadFaill/9009bd97a8d109cf94b0 to your computer and use it in GitHub Desktop.
python ext example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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."); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import foo | |
print foo.add(1, 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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