Last active
September 13, 2018 07:19
-
-
Save zed/5330891 to your computer and use it in GitHub Desktop.
swig hello world 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
/_hello*.so | |
/build/ | |
/hello.py | |
/hello_wrap.c | |
/MANIFEST | |
/dist/ |
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 <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; | |
} |
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
double doublefun(double b); | |
float floatfun(float b); | |
int intfun(int b); |
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
%module hello | |
%{ | |
#include "hello.h" | |
%} | |
%include "hello.h" |
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
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 |
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 hello.h | |
include hello.c | |
include hello.py | |
include hello_wrap.c | |
include setup.py |
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 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"], | |
) |
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 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