Reusing python/cython code from static lib?

102 views
Skip to first unread message

Sour Ce

unread,
Jan 3, 2021, 4:33:51 AM1/3/21
to cython-users
Hey again guys. I'm wondering if it's possible to compile python/cython code as a static lib and reuse it from python/cython?
I've exhausted questions/answers from this group, cython docs, SO and Google to the best of my ability.

So far (over the past couple of years) I've gotten everything else involving Cython except using cython static libs to work:
* Compiling to and reusing python/cython code from shared libs
* Pure C/C++ using extern from
* Compiling and reusing pure C/C++ code from a static lib using extern from
* "Including" and embedding a large codebase as a cython standalone

Things I've tried to reuse cython code from static lib:
* cdef extern void test()
* cdef extern from "cythonlib.h":
    void test()
* cimport cythonlib
* include "cythonlib.pxd"
* include "cythonlib.pyx"

The error I get with extern/extern from is: /usr/bin/ld: /tmp/ccaKkuJb.o: in function `__pyx_pymod_exec_run(_object*)':
run.cpp:(.text+0x8d4): undefined reference to `test()'

The only thing that's worked for me as I said is rewriting as pure C++ and linking to this cpplib.a:
run.pyx:
cdef extern from "cpplib.h":
        void test()
test()

If I use this same run.pyx file/definition/content as above but link to cythonlib.a instead (same signature, just Cython vs pure C++ code) I get the same error as above.

PS: I highly avoid using distutils due to its clunkiness, so a general answer/explanation or one using binutils is highly preferred if possible.

da-woods

unread,
Jan 4, 2021, 3:01:06 AM1/4/21
to cython...@googlegroups.com
One big problem here is going to be that Cython relies on the module (i.e. the contents of your static library) having been initialized properly. In the past you could solve this by calling PyInit_modulename, however this was apparently never right, and is now definitely wrong and will break things.

For embedding in C/C++ the recommendation is that you add the initialization function to Python's inittab (https://docs.python.org/3/c-api/import.html#c._inittab). However this must be done before Python is started so doesn't help when trying to statically link this into another Cython module.

There's a few recipes available to try to bundle multiple modules into one extension (which I think is basically what you want to do). https://github.com/smok-serwis/snakehouse tries to automate it. Otherwise you'll have to become pretty familiar with the fine details of https://www.python.org/dev/peps/pep-0489/

Sorry that's a bit vague - I don't really have a comprehensive solution but hopefully it points you in a helpful direction.
--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/038a3063-9826-459e-bb96-7ea0678e335bn%40googlegroups.com.


Sour Ce

unread,
Jan 26, 2021, 1:42:08 AM1/26/21
to cython-users
I was hoping there would be an out of box solution to compiling static libs just about the same way dynamic libs are compiled with cythonize.
Not very significant to me if there isn't, was hoping maybe I just did some obvious mistake, the idea here was just to try to reduce compile times of the project by splitting the project up into modules and utilizing multicore compilation either through cythonize or manually running build commands instead of using cython include statements which is only compiled on a single core.

I'm honestly too inexperienced to understand your suggestions, I'm using cython basically to try learn Python as well as C/C++ in one go
Reply all
Reply to author
Forward
0 new messages