我正在构建一个特殊用途的嵌入式 Python 解释器,并希望避免依赖动态库,所以我想用静态库编译解释器(例如 libc.a
而不是 libc.so
).
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 (e.g. libc.a
not libc.so
).
我还想静态链接所有属于 Python 标准库的动态库.我知道这可以使用 Freeze.py
来完成,但是有没有替代方法可以一步完成?
I would also like to statically link all dynamic libraries that are part of the Python standard library. I know this can be done using Freeze.py
, but is there an alternative so that it can be done in one step?
我发现了这个(主要是关于Python模块的静态编译):
I found this (mainly concerning static compilation of Python modules):
描述用于配置的文件位于此处:
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
Setup
文件中包含大量文档,源代码中包含的 README
也提供了许多很好的编译信息.
The Setup
file has tons of documentation in it and the README
included with the source offers lots of good compilation information as well.
我还没有尝试编译,但我认为有了这些资源,我尝试时应该会成功.我会在这里发表我的结果作为评论.
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.
要获得纯静态的python可执行文件,还必须进行如下配置:
To get a pure-static python executable, you must also configure as follows:
./configure LDFLAGS="-static -static-libgcc" CPPFLAGS="-static"
在启用这些标志的情况下进行构建后,您可能会收到很多关于由于库不存在而重命名"的警告.这意味着您没有正确配置Modules/Setup
,需要:
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:
a) 添加一行(靠近顶部),如下所示:
a) add a single line (near the top) like this:
*static*
(即星号/星号静态"和星号不带空格)
(that's asterisk/star the word "static" and asterisk with no spaces)
b) 取消注释您希望静态可用的所有模块(例如数学、数组等...)
b) uncomment all modules that you want to be available statically (such as math, array, etc...)
您可能还需要添加特定的链接器标志(如我上面发布的链接中所述).到目前为止,我的经验是这些库无需修改即可运行.
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.
使用以下命令运行 make 也可能会有所帮助:
It may also be helpful to run make with as follows:
make 2>&1 | grep 'renaming'
这将显示所有由于静态链接而无法编译的模块.
This will show all modules that are failing to compile due to being statically linked.
这篇关于静态编译 Python 解释器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!