Can I statically link Cython modules into an execu

2019-04-29 06:45发布

I currently have an executable compiled from C++ that embeds python. The embedded executable runs a python script which load several Cython modules. Both the Cython modules and the executable are linked against a shared library.

I want to move the shared library into the executable by statically linking the shared library against the executable.

Can I statically link the Cython modules into the executable which embeds python? What is the best way to handle this situation?

1条回答
2楼-- · 2019-04-29 07:25

Yes it's possible, but if you have an hand on the python interpreter. What i'm going to describe have been done for python on IOS platform. You need to check more how to let python known about your module if you don't want to touch on the original python interpreter (Replace TEST everywhere with your own tag/libname)

One possible way to do it is:

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

  • Create an libTEST.a, including all the .o generated during the build process (not the .so). You can found it usually in the 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 that new libTEST.a by appending in the compilation command line:

    -lTEST -L.

The result will give you an executable with all the symbol from your cython modules, and python will be able to search them in memory.

(As an example, I'm using an enhanced wrapper that redirect ld during compilation to not produce .so, and create a .a at the end. On the kivy-ios project, you can grab liblink that is used to produce .o, and biglink that is used to grab all the .o in directories and produce .a. You can see how it's used in build_kivy.sh)

查看更多
登录 后发表回答