public
Last active

swig hello world example

  • Download Gist
.gitignore
1 2 3 4 5 6
/_hello*.so
/build/
/hello.py
/hello_wrap.c
/MANIFEST
/dist/
MANIFEST.in
1 2 3 4 5
include hello.h
include hello.c
include hello.py
include hello_wrap.c
include setup.py
Makefile
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
python=python
 
all: test_hello
 
test_hello: test_hello.py _hello.so
$(python) test_hello.py
 
_hello.so: setup.py hello_wrap.c hello.h hello.c hello.i
$(python) setup.py build_ext --inplace
 
hello.py hello_wrap.c: hello.i
swig -python -o hello_wrap.c $<
 
sdist: setup.py hello.py hello_wrap.c
$(python) setup.py sdist
clean:
-rm hello.py hello_wrap.c _hello*.so *.pyc MANIFEST
-rm build/ dist/ __pycache__/ -rf
 
.PHONY: all test_hello clean sdist
hello.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <stdio.h>
#include "hello.h"
 
double doublefun(double b){
printf("c(%g)",b);
return b+12.5;
}
 
float floatfun(float b){
printf("c(%f)",b);
return b+12.5;
}
 
int intfun(int b){
printf("c(%d)",b);
return b+12;
}
hello.h
C
1 2 3
double doublefun(double b);
float floatfun(float b);
int intfun(int b);
hello.i
1 2 3 4 5 6 7
%module hello
 
%{
#include "hello.h"
%}
 
%include "hello.h"
setup.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import os
from distutils.core import setup, Extension
 
ext_module = Extension('_hello',
sources=['hello_wrap.c', 'hello.c'],
depends=['hello.h'],
)
 
setup (name = 'swig-hello-world-example',
version = '0.1',
description = "swig hello world example",
ext_modules = [ext_module],
py_modules = ["hello"],
)
test_hello.py
Python
1 2 3 4 5
import hello
 
print("i: %s" % hello.intfun(2))
print("f: %s" % hello.floatfun(2.3))
print("d: %s" % hello.doublefun(2.3))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.