Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a static Cython library using distutils?

I'd like to build a static Cython library using distutils. I don't care about having it be a true Python extension module that can be import'ed. I just want to compile the code and put the objects in a static library. The code to create a dynamic library is very simple,

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
   cmdclass = {'build_ext':build_ext},
   ext_modules = [Extension("test",["test.pyx"])]
)

Is there a simple way to make it static instead?

like image 399
marius Avatar asked Apr 04 '14 00:04

marius


People also ask

What C compiler does Cython use?

MSVC is the only compiler that Cython is currently tested with on Windows. A possible alternative is the open source MinGW (a Windows distribution of gcc).

Does Cython need to be compiled?

Cython source file names consist of the name of the module followed by a . pyx extension, for example a module called primes would have a source file named primes. pyx . Cython code, unlike Python, must be compiled.

Does Cython compile to C?

The Cython language is a superset of Python that compiles to C, yielding performance boosts that can range from a few percent to several orders of magnitude, depending on the task at hand. For work that is bound by Python's native object types, the speedups won't be large.

How do I compile Python to Cython?

To make your Python into Cython, first you need to create a file with the . pyx extension rather than the . py extension. Inside this file, you can start by writing regular Python code (note that there are some limitations in the Python code accepted by Cython, as clarified in the Cython docs).


1 Answers

Distutils is very limited and not set up for static builds. I would advise you to use something else to compile the static library part of your project.

If your use case is to call into Cython code from other C code, then you want to use the public or api declarations along with your cdef declared functions and variables in your Cython code. Cython will allow the so-declared objects to be called from external C code, and it will generate a .h file alongside the .c file for you.

like image 59
lothario Avatar answered Sep 20 '22 20:09

lothario