Library linking demo.
authorRobert Bradshaw <robertwb@math.washington.edu>
Fri, 22 Jan 2010 06:31:20 +0000 (22:31 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Fri, 22 Jan 2010 06:31:20 +0000 (22:31 -0800)
Demos/libraries/call_mymath.pyx [new file with mode: 0644]
Demos/libraries/mymath.c [new file with mode: 0644]
Demos/libraries/mymath.h [new file with mode: 0644]
Demos/libraries/setup.py [new file with mode: 0644]

diff --git a/Demos/libraries/call_mymath.pyx b/Demos/libraries/call_mymath.pyx
new file mode 100644 (file)
index 0000000..1b45f80
--- /dev/null
@@ -0,0 +1,5 @@
+cdef extern from "mymath.h":
+    double sinc(double)
+
+def call_sinc(x):
+    return sinc(x)
diff --git a/Demos/libraries/mymath.c b/Demos/libraries/mymath.c
new file mode 100644 (file)
index 0000000..b16ed5a
--- /dev/null
@@ -0,0 +1,5 @@
+#include "math.h"
+
+double sinc(double x) {
+    return x == 0 ? 1 : sin(x)/x;
+}
\ No newline at end of file
diff --git a/Demos/libraries/mymath.h b/Demos/libraries/mymath.h
new file mode 100644 (file)
index 0000000..4f2bb4b
--- /dev/null
@@ -0,0 +1 @@
+double sinc(double);
diff --git a/Demos/libraries/setup.py b/Demos/libraries/setup.py
new file mode 100644 (file)
index 0000000..b4a3659
--- /dev/null
@@ -0,0 +1,31 @@
+import os
+
+from distutils.core import setup
+from distutils.extension import Extension
+from Cython.Distutils import build_ext
+
+
+# For demo purposes, we build our own tiny library.
+try:
+    print "building libmymath.a"
+    assert os.system("gcc -c mymath.c -o mymath.o") == 0
+    assert os.system("ar rcs libmymath.a mymath.o") == 0
+except:
+    if not os.path.exists("libmymath.a"):
+        print "Error building external library, please create libmymath.a manually."
+        sys.exit(1)
+
+# Here is how to use the library built above. 
+ext_modules=[ 
+    Extension("call_mymath",
+              sources = ["call_mymath.pyx"],
+              include_dirs = [os.getcwd()],  # path to .h file(s)
+              library_dirs = [os.getcwd()],  # path to .a or .so file(s)
+              libraries = ['mymath'])
+]
+
+setup(
+  name = 'Demos',
+  cmdclass = {'build_ext': build_ext},
+  ext_modules = ext_modules,
+)