add a simple swig example (from the turorial)
parent
710e4c3657
commit
f56ff3c1bc
@ -0,0 +1,5 @@
|
||||
*.pyc
|
||||
*.o
|
||||
*.so
|
||||
test_swig.py
|
||||
test_swig_wrap.c
|
@ -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
|
@ -0,0 +1,20 @@
|
||||
/* Example from the SWIG tutorial */
|
||||
|
||||
#include <time.h>
|
||||
|
||||
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);
|
||||
}
|
@ -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();
|
@ -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)
|
Loading…
Reference in New Issue