From 12d1807d1e8f453745e4f4abf25209b57909bca5 Mon Sep 17 00:00:00 2001 From: neingeist Date: Mon, 5 May 2014 14:55:58 +0200 Subject: [PATCH] add a simple swig example (from the turorial) --- Makefile | 22 ++++++++++++++++++++++ test_swig.c | 20 ++++++++++++++++++++ test_swig.i | 14 ++++++++++++++ test_swig_test.py | 15 +++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 Makefile create mode 100644 test_swig.c create mode 100644 test_swig.i create mode 100644 test_swig_test.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6e4c498 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +.PHONY: default +default: all test + +.PHONY: all +all: _test_swig.so + +.PHONY: test +test: + python2.7 test_swig_test.py + +_test_swig.so: test_swig.o + ld -shared test_swig.o test_swig_wrap.o -o _test_swig.so + +test_swig.o: test_swig_wrap.c + gcc -fpic -c test_swig.c test_swig_wrap.c -I/usr/include/python2.7 + +test_swig_wrap.c: test_swig.i + swig -python $< + +.PHONY: clean +clean: + rm -f *.o *.so test_swig_wrap.c *.pyc test_swig.py diff --git a/test_swig.c b/test_swig.c new file mode 100644 index 0000000..2617bca --- /dev/null +++ b/test_swig.c @@ -0,0 +1,20 @@ +/* Example from the SWIG tutorial */ + +#include + +double My_variable = 3.0; + +int fact(int n) { + if (n <= 1) return 1; + else return n*fact(n-1); +} + +int my_mod(int x, int y) { + return (x%y); +} + +char *get_time() { + time_t ltime; + time(<ime); + return ctime(<ime); +} diff --git a/test_swig.i b/test_swig.i new file mode 100644 index 0000000..54b67f0 --- /dev/null +++ b/test_swig.i @@ -0,0 +1,14 @@ + /* Example from the SWIG tutorial */ + %module test_swig + %{ + /* Put header files here or function declarations like below */ + extern double My_variable; + extern int fact(int n); + extern int my_mod(int x, int y); + extern char *get_time(); + %} + + extern double My_variable; + extern int fact(int n); + extern int my_mod(int x, int y); + extern char *get_time(); \ No newline at end of file diff --git a/test_swig_test.py b/test_swig_test.py new file mode 100644 index 0000000..0cb5409 --- /dev/null +++ b/test_swig_test.py @@ -0,0 +1,15 @@ +from __future__ import division, print_function +import test_swig + +# FIXME +#print(test_swig.My_variable) +#assert(test_swig.My_variable == 3.0) + +print(test_swig.fact(5)) +assert(test_swig.fact(5) == 120) + +print(test_swig.my_mod(23, 5)) +assert(test_swig.my_mod(23, 5) == 3) + +print(test_swig.get_time(), end="") +assert(len(test_swig.get_time()) > 20)