简体   繁体   English

静态编译Python解释器?

[英]Compile the Python interpreter statically?

I'm building a special-purpose embedded Python interpreter and want to avoid having dependencies on dynamic libraries so I want to compile the interpreter with static libraries instead (eg libc.a not libc.so ). 我正在构建一个专用的嵌入式Python解释器,并希望避免依赖于动态库,所以我想用静态库编译解释器(例如libc.a而不是libc.so )。

I would also like to statically link all dynamic libraries that are part of the Python standard library. 我还想静态链接属于Python标准库的所有动态库。 I know this can be done using Freeze.py , but is there an alternative so that it can be done in one step? 我知道这可以使用Freeze.py来完成,但有没有其他方法可以一步完成?

I found this (mainly concerning static compilation of Python modules): 我发现了这个(主要是关于Python模块的静态编译):

Which describes a file used for configuration located here: 其中描述了用于配置的文件:

<Python_Source>/Modules/Setup

If this file isn't present, it can be created by copying: 如果此文件不存在,可以通过复制创建:

<Python_Source>/Modules/Setup.dist

The Setup file has tons of documentation in it and the README included with the source offers lots of good compilation information as well. Setup文件中包含大量文档,源代码中包含的README提供了大量良好的编译信息。

I haven't tried compiling yet, but I think with these resources, I should be successful when I try. 我还没有尝试编译,但我认为有了这些资源,我尝试时应该会成功。 I will post my results as a comment here. 我会在此发表评论结果。

Update 更新

To get a pure-static python executable, you must also configure as follows: 要获得纯静态python可执行文件,还必须配置如下:

./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-static"

Once you build with these flags enabled, you will likely get lots of warnings about "renaming because library isn't present". 一旦你启用这些标志构建,你可能会收到很多关于“重命名因为库不存在”的警告。 This means that you have not configured Modules/Setup correctly and need to: 这意味着您尚未正确配置Modules/Setup ,需要:

a) add a single line (near the top) like this: a)添加一行(靠近顶部),如下所示:

*static*

(that's asterisk/star the word "static" and asterisk with no spaces) (这是星号/星号“静态”和星号,没有空格)

b) uncomment all modules that you want to be available statically (such as math, array, etc...) b)取消注释您想要静态可用的所有模块(例如数学,数组等......)

You may also need to add specific linker flags (as mentioned in the link I posted above). 您可能还需要添加特定的链接器标志(如上面发布的链接中所述)。 My experience so far has been that the libraries are working without modification. 到目前为止,我的经验是图书馆正在不加修改地工作。

It may also be helpful to run make with as follows: 运行make可能也有帮助,如下所示:

make 2>&1 | grep 'renaming'

This will show all modules that are failing to compile due to being statically linked. 这将显示由于静态链接而无法编译的所有模块。

CPython CMake Buildsystem offers an alternative way to build Python, using CMake . CPython CMake Buildsystem提供了一种使用CMake构建Python的替代方法。

It can build python lib statically, and include in that lib all the modules you want. 它可以静态地构建python lib,并在该lib中包含您想要的所有模块。 Just set CMake's options 只需设置CMake的选项

BUILD_SHARED                     OFF
BUILD_STATIC                     ON

and set the BUILTIN_<extension> you want to ON . 并设置要ONBUILTIN_<extension>

Using freeze doesn't prevent doing it all in one run (no matter what approach you use, you will need multiple build steps - eg many compiler invocations). 使用freeze并不会阻止在一次运行中完成所有操作(无论使用何种方法,您都需要多个构建步骤 - 例如,许多编译器调用)。 First, you edit Modules/Setup to include all extension modules that you want. 首先,编辑Modules/Setup以包含所需的所有扩展模块。 Next, you build Python, getting libpythonxy.a. 接下来,构建Python,获取libpythonxy.a。 Then, you run freeze, getting a number of C files and a config.c. 然后,您运行freeze,获取许多C文件和config.c。 You compile these as well, and integrate them into libpythonxy.a (or create a separate library). 您也可以编译它们,并将它们集成到libpythonxy.a中(或创建一个单独的库)。

You do all this once, for each architecture and Python version you want to integrate. 对于要集成的每个体系结构和Python版本,您只需执行一次此操作。 When building your application, you only link with libpythonxy.a, and the library that freeze has produced. 构建应用程序时,只链接libpythonxy.a和冻结的库。

You can try with ELF STATIFIER . 您可以尝试使用ELF STATIFIER I've been used it before and it works fairly well. 我以前一直在使用它,它运作得相当好。 I just had problems with it in a couple of cases and then I had to use another similar program called Ermine . 我在几个案例中遇到了问题,然后我不得不使用另一个名为Ermine的类似程序。 Unfortunately this one is a commercial program. 不幸的是,这是一个商业计划。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM