Skip to content

Instantly share code, notes, and snippets.

@zed
Last active September 13, 2018 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zed/5330891 to your computer and use it in GitHub Desktop.
Save zed/5330891 to your computer and use it in GitHub Desktop.
swig hello world example
/_hello*.so
/build/
/hello.py
/hello_wrap.c
/MANIFEST
/dist/
#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;
}
double doublefun(double b);
float floatfun(float b);
int intfun(int b);
%module hello
%{
#include "hello.h"
%}
%include "hello.h"
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
include hello.h
include hello.c
include hello.py
include hello_wrap.c
include setup.py
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"],
)
import hello
print("i: %s" % hello.intfun(2))
print("f: %s" % hello.floatfun(2.3))
print("d: %s" % hello.doublefun(2.3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment