From 36c08c89eb7d23b2a33df48f40d96441422292a1 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Thu, 21 Jan 2010 22:31:20 -0800 Subject: [PATCH] Library linking demo. --- Demos/libraries/call_mymath.pyx | 5 +++++ Demos/libraries/mymath.c | 5 +++++ Demos/libraries/mymath.h | 1 + Demos/libraries/setup.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 Demos/libraries/call_mymath.pyx create mode 100644 Demos/libraries/mymath.c create mode 100644 Demos/libraries/mymath.h create mode 100644 Demos/libraries/setup.py diff --git a/Demos/libraries/call_mymath.pyx b/Demos/libraries/call_mymath.pyx new file mode 100644 index 00000000..1b45f804 --- /dev/null +++ b/Demos/libraries/call_mymath.pyx @@ -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 index 00000000..b16ed5ae --- /dev/null +++ b/Demos/libraries/mymath.c @@ -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 index 00000000..4f2bb4b7 --- /dev/null +++ b/Demos/libraries/mymath.h @@ -0,0 +1 @@ +double sinc(double); diff --git a/Demos/libraries/setup.py b/Demos/libraries/setup.py new file mode 100644 index 00000000..b4a3659a --- /dev/null +++ b/Demos/libraries/setup.py @@ -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, +) -- 2.26.2