cython-devel

changeset 2813:fce98451e2dd

Library linking demo.
author Robert Bradshaw <robertwb@math.washington.edu>
date Thu Jan 21 22:31:20 2010 -0800 (2 years ago)
parents 5215ebde137d
children 43e126948c79
files Demos/libraries/call_mymath.pyx Demos/libraries/mymath.c Demos/libraries/mymath.h Demos/libraries/setup.py
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Demos/libraries/call_mymath.pyx Thu Jan 21 22:31:20 2010 -0800 1.3 @@ -0,0 +1,5 @@ 1.4 +cdef extern from "mymath.h": 1.5 + double sinc(double) 1.6 + 1.7 +def call_sinc(x): 1.8 + return sinc(x)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/Demos/libraries/mymath.c Thu Jan 21 22:31:20 2010 -0800 2.3 @@ -0,0 +1,5 @@ 2.4 +#include "math.h" 2.5 + 2.6 +double sinc(double x) { 2.7 + return x == 0 ? 1 : sin(x)/x; 2.8 +} 2.9 \ No newline at end of file
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/Demos/libraries/mymath.h Thu Jan 21 22:31:20 2010 -0800 3.3 @@ -0,0 +1,1 @@ 3.4 +double sinc(double);
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/Demos/libraries/setup.py Thu Jan 21 22:31:20 2010 -0800 4.3 @@ -0,0 +1,31 @@ 4.4 +import os 4.5 + 4.6 +from distutils.core import setup 4.7 +from distutils.extension import Extension 4.8 +from Cython.Distutils import build_ext 4.9 + 4.10 + 4.11 +# For demo purposes, we build our own tiny library. 4.12 +try: 4.13 + print "building libmymath.a" 4.14 + assert os.system("gcc -c mymath.c -o mymath.o") == 0 4.15 + assert os.system("ar rcs libmymath.a mymath.o") == 0 4.16 +except: 4.17 + if not os.path.exists("libmymath.a"): 4.18 + print "Error building external library, please create libmymath.a manually." 4.19 + sys.exit(1) 4.20 + 4.21 +# Here is how to use the library built above. 4.22 +ext_modules=[ 4.23 + Extension("call_mymath", 4.24 + sources = ["call_mymath.pyx"], 4.25 + include_dirs = [os.getcwd()], # path to .h file(s) 4.26 + library_dirs = [os.getcwd()], # path to .a or .so file(s) 4.27 + libraries = ['mymath']) 4.28 +] 4.29 + 4.30 +setup( 4.31 + name = 'Demos', 4.32 + cmdclass = {'build_ext': build_ext}, 4.33 + ext_modules = ext_modules, 4.34 +)