I have never thought it could be that simple to make a python module in C until I met SWIG.
Here you are my simple example.c file:
int add(int a, int b) {
return a + b;
}
int mul(int a, int b) {
return a * b;
}
Now create a new file and call it example.i and add definitions to the functions:
%module example
%{
%}
int add(int a, int b);
int mul(int a, int b);
Now compile as following:
swig -python example.i
cc -c example.c example_wrap.c -I/usr/include/python2.6
ld -shared example.o example_wrap.o -o _example.so
Now you have a module. Open python and import example and use it:
example.add(1,2)
No comments:
Post a Comment