Can I statically link Cython modules to an executable that embeds python?

I currently have an executable compiled from C ++ that includes python. The built-in executable runs a python script that loads several Cython modules. Both Cython modules and the executable are linked to a common library.

I want to move the shared library to an executable, statically linking the shared library to the executable.

Can I statically link Cython modules to an executable that embeds python? What is the best way to deal with this situation?

+4
python cython static-libraries
Dec 03 2018-11-12T00:
source share
1 answer

Yes, it is possible, but if you have a hand on the python interpreter. What I will tell was done for python on the IOS platform. You need to check again how to tell python about your module if you don't want to touch the original python interpreter (replace TEST everywhere with your own / libname tag).

One possible way to do this is:

  • Compile your own python with the dynload patch , which prefers not to dlopen () your module, but use dlsym () directly to check if the module is in memory.

  • Create libTEST.a, including all .o created during the build process (not .so). You can usually find it in build/temp.* And do something like this:

     ar rc libTEST.a build/temp.*/*.o ranlib libTEST.a 
  • When compiling the main executable, you need to add a dependency to this new libTEST.a by adding to the compilation command line:

    -lTEST -L.

The result will provide you with an executable with all the character from your cython modules, and python will be able to look for them in memory.

(As an example, I use an extended shell that redirects ld at compile time so as not to create .so and create .a at the end. On kivy-ios , you can grab the liblink that is used to create the .o and biglink that is used to capture all .o in directories and create .a. You can see how it is used in build_kivy.sh )

+7
Dec 06 '11 at 11:16
source share



All Articles