From: Robert Bradshaw Date: Fri, 22 Jan 2010 06:31:20 +0000 (-0800) Subject: Library linking demo. X-Git-Tag: 0.12.1~15 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=36c08c89eb7d23b2a33df48f40d96441422292a1;p=cython.git Library linking demo. --- 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, +)