I have been developing a cython function inside an Ipython notebook using the cell magic %%cython
to compile my code. This works successfully, and I am able to call the compiled function in cells without the cython magic present.
I now wish to be able to compile my cython function to a (library, object?) such that I am able to import it into pure python scripts, via import <foo>
.
My problem is that I am at a loss in terms of how to use distutils
properly to specify the correct setup options. For example, my %%cython
cell magic is:
%%cython -lgsl -L/usr/lib64/atlas -ltatlas -lm
And I am not sure how to pass these arguments to gcc from distutils.
I also pass extra arguments (flags?), at the beginning of the cython maigic cell as follows:
import Cython.Compiler.Options as CO
CO.extra_compile_args = ["-f",'-O3',"-ffast-math","openmp","march=native"]
CO.extra_link_args = ["-fopenmp"]
I realise some of these may be unnecessary but the code is compiling and running correctly inside the notebook.
Since my function also calls numpy and scipy, I am aware I need to link these libraries, too.
I have attempted to compile the function via gcc by first running (from the command line):
cython <my_func>.pyx --embed
And then:
gcc -O3 -fopenmp -ffast-math <my_func>.c -I/usr/include/python2.7 -I<numpy_include> -L<numpy-location> -L/usr/lib64/atlas -lgsl -ltatlas -lm -lpython2.7 -o <my_func>.so
Which successfully produces a <my_func>.so
, but I am then unable to access anything inside it from python.
Importing <my_func>.so
as a C-library with Ctypes does not work either as Ctypes is unable to find my function in the shared object.
I realise constructing a setup.py
script using distutils
is probably the "correct" way to go about this, but I have been unable to find any resources explaining how to link and include numpy, scipy and some C-libraries (GSL) while passing additional compile flags to gcc.
What is the best way to compile and call this cython function from outside a notebook?