Python中static相关知识小结


Posted in Python onJanuary 02, 2018

非 static 编译

不指定额外参数直接编译 Python:

$ ./configure
$ make

查看所依赖的共享库:

$ ldd python
  linux-vdso.so.1 => (0x00007fffcd95a000)
  libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fab5c350000)
  libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fab5c140000)
  libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fab5bf20000)
  libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fab5bc10000)
  libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fab5b840000)
  /lib64/ld-linux-x86-64.so.2 (0x00007fab5c600000)

static 编译

指定 static 相关参数编译 Python:

$ ./configure LDFLAGS="-static" --disable-shared
$ make LDFLAGS="-static" LINKFORSHARED=" "

发现有部分库未能被编译,编译结果中输出如下内容:

Failed to build these modules:
_bisect      _bsddb       _codecs_cn
_codecs_hk     _codecs_iso2022  _codecs_jp
_codecs_kr     _codecs_tw     _collections
_csv        _ctypes      _ctypes_test
_curses      _curses_panel   _elementtree
_functools     _hashlib      _heapq
_hotshot      _io        _json
_locale      _lsprof      _multibytecodec
_multiprocessing  _random      _socket
_sqlite3      _ssl        _struct
_testcapi     _tkinter      array
audioop      binascii      bz2
cmath       cPickle      crypt
cStringIO     datetime      dbm
fcntl       future_builtins  gdbm
grp        itertools     linuxaudiodev
math        mmap        nis
operator      ossaudiodev    parser
pyexpat      readline      resource
select       spwd        strop
syslog       termios      time
unicodedata    zlib

查看所依赖的共享库:

$ ldd python
  not a dynamic executable

static 编译相关 module

编辑 Modules/Setup.local 为:

# Edit this file for local setup changes
*static*
_bisect _bisectmodule.c
# _bsddb _bsddb.c # 额外依赖PyBSDDB
_codecs_cn cjkcodecs/_codecs_cn.c
_codecs_hk cjkcodecs/_codecs_hk.c
_codecs_iso2022 cjkcodecs/_codecs_iso2022.c
_codecs_jp cjkcodecs/_codecs_jp.c
_codecs_kr cjkcodecs/_codecs_kr.c
_codecs_tw cjkcodecs/_codecs_tw.c
_collections _collectionsmodule.c
_csv _csv.c
# _ctypes _ctypes/_ctypes.c _ctypes/callbacks.c _ctypes/callproc.c _ctypes/cfield.c _ctypes/malloc_closure.c _ctypes/stgdict.c # 还依赖Python源中的 libffi
# _ctypes_test # 同_ctypes
# _curses _cursesmodule.c # 额外依赖ncurses
# _curses_panel _curses_panel.c # 额外依赖ncurses
# _elementtree _elementtree.c expat/loadlibrary.c expat/xmlparse.c expat/xmlrole.c expat/xmltok.c expat/xmltok_impl.c expat/xmltok_ns.c # 依赖的libexpat可能编译出错
_functools _functoolsmodule.c
# _hashlib _hashopenssl.c # 额外依赖OpenSSL
_heapq _heapqmodule.c
_hotshot _hotshot.c
_io _io/_iomodule.c _io/bufferedio.c _io/bytesio.c _io/fileio.c _io/iobase.c _io/stringio.c _io/textio.c
_json _json.c
_locale _localemodule.c
_lsprof _lsprof.c rotatingtree.c
_multibytecodec cjkcodecs/multibytecodec.c
# _multiprocessing _multiprocessing/multiprocessing.c _multiprocessing/pipe_connection.c _multiprocessing/semaphore.c _multiprocessing/socket_connection.c _multiprocessing/win32_functions.c
_multiprocessing _multiprocessing/multiprocessing.c _multiprocessing/socket_connection.c
_random _randommodule.c
_socket socketmodule.c
# _sqlite3 _sqlite/cache.c _sqlite/connection.c _sqlite/cursor.c _sqlite/microprotocols.c _sqlite/module.c _sqlite/prepare_protocol.c _sqlite/row.c _sqlite/statement.c _sqlite/util.c # 额外依赖sqlite3 
# _ssl _ssl.c 额外依赖OpenSSL
_struct _struct.c
_testcapi _testcapimodule.c
# _tkinter _tkinter.c # 额外依赖Tcl/Tk
array arraymodule.c
audioop audioop.c
binascii binascii.c
# bz2 bz2module.c # 额外依赖bzlib
cmath cmathmodule.c
cPickle cPickle.c
# crypt cryptmodule.c # 额外依赖OpenSSL
cStringIO cStringIO.c
datetime datetimemodule.c
# dbm dbmmodule.c # 额外依赖gdbm
fcntl fcntlmodule.c
future_builtins future_builtins.c
# gdbm gdbmmodule.c # 额外依赖gdbm
grp grpmodule.c
itertools itertoolsmodule.c
linuxaudiodev linuxaudiodev.c
math mathmodule.c _math.c
mmap mmapmodule.c
# nis nismodule.c # 依赖glibc/nis
operator operator.c
ossaudiodev ossaudiodev.c
parser parsermodule.c
# pyexpat pyexpat.c # 依赖的libexpat可能编译出错
# readline readline.c # 依赖readline
resource resource.c
select selectmodule.c
spwd spwdmodule.c
strop stropmodule.c
syslog syslogmodule.c
termios termios.c
time timemodule.c
unicodedata unicodedata.c
zlib zlibmodule.c zlib/adler32.c zlib/compress.c zlib/crc32.c zlib/deflate.c zlib/example.c zlib/gzclose.c zlib/gzlib.c zlib/gzread.c zlib/gzwrite.c zlib/infback.c zlib/inffast.c zlib/inflate.c zlib/inftrees.c zlib/minigzip.c zlib/trees.c zlib/uncompr.c zlib/zutil.c

重新编译后能将部分 Modules 编译为 static 库,输出:

Failed to build these modules:
_bsddb     _ctypes     _ctypes_test
_curses     _curses_panel  _elementtree
_hashlib    _sqlite3    _ssl
_tkinter    bz2       crypt
dbm       gdbm      nis
pyexpat     readline

查看所依赖的共享库:

$ ldd python
  not a dynamic executable

参考:

https://wiki.python.org/moin/BuildStatically

Python 相关文章推荐
Python中最常用的操作列表的几种方法归纳
Apr 24 Python
利用python模拟实现POST请求提交图片的方法
Jul 25 Python
Python使用正则表达式过滤或替换HTML标签的方法详解
Sep 25 Python
matplotlib在python上绘制3D散点图实例详解
Dec 09 Python
python实现决策树
Dec 21 Python
Python 实现中值滤波、均值滤波的方法
Jan 09 Python
Django中多种重定向方法使用详解
Jul 17 Python
浅谈django 模型类使用save()方法的好处与注意事项
Mar 28 Python
简单了解Python多态与属性运行原理
Jun 15 Python
python如何操作mysql
Aug 17 Python
python批量检查两个对应的txt文件的行数是否一致的实例代码
Oct 31 Python
Django项目配置Memcached和Redis, 缓存选择哪个更有优势
Apr 06 Python
python tensorflow基于cnn实现手写数字识别
Jan 01 #Python
python+selenium实现163邮箱自动登陆的方法
Dec 31 #Python
python 类对象和实例对象动态添加方法(分享)
Dec 31 #Python
利用python将图片转换成excel文档格式
Dec 30 #Python
书单|人生苦短,你还不用python!
Dec 29 #Python
python ansible服务及剧本编写
Dec 29 #Python
详解python 拆包可迭代数据如tuple, list
Dec 29 #Python
You might like
从MySQL数据库表中取出随机数据的代码
2007/09/05 PHP
PHP简洁函数(PHP简单明了函数语法)
2012/06/10 PHP
单一index.php实现PHP任意层级文件夹遍历(Zjmainstay原创)
2012/07/31 PHP
php获取操作系统语言代码
2013/11/04 PHP
php使用for语句输出三角形的方法
2015/06/09 PHP
php多进程并发编程防止出现僵尸进程的方法分析
2020/02/28 PHP
JQUERY设置IFRAME的SRC值的代码
2010/11/30 Javascript
node.js使用nodemailer发送邮件实例
2014/03/10 Javascript
JavaScript String(字符串)对象的简单实例(推荐)
2016/08/31 Javascript
vue2.0父子组件及非父子组件之间的通信方法
2017/01/21 Javascript
jQuery移除或禁用html元素点击事件常用方法小结
2017/02/10 Javascript
微信小程序 数据遍历的实现
2017/04/05 Javascript
vue与bootstrap实现时间选择器的示例代码
2017/08/26 Javascript
JS+CSS实现网页加载中的动画效果
2017/10/27 Javascript
JS获取今天是本月第几周、本月共几周、本月有多少天、是今年的第几周、是今年的第几天的示例代码
2018/12/05 Javascript
40行代码把Vue3的响应式集成进React做状态管理
2020/05/20 Javascript
浅谈element中InfiniteScroll按需引入的一点注意事项
2020/06/05 Javascript
[02:54]辉夜杯主赛事第二日败者组 iG.V赛后采访
2015/12/26 DOTA
使用graphics.py实现2048小游戏
2015/03/10 Python
Pycharm导入Python包,模块的图文教程
2018/06/13 Python
python+opencv实现霍夫变换检测直线
2020/10/23 Python
几个适合python初学者的简单小程序,看完受益匪浅!(推荐)
2019/04/16 Python
对python3中的RE(正则表达式)-详细总结
2019/07/23 Python
python logging 日志的级别调整方式
2020/02/21 Python
python如何实现DES加密
2020/09/21 Python
Django2.1.7 查询数据返回json格式的实现
2020/12/29 Python
html5开发之viewport使用
2013/10/17 HTML / CSS
美国棒球装备和用品商店:Baseball Savings
2018/06/09 全球购物
20世纪40年代连衣裙和复古服装:The Seamstress Of Bloomsbury
2018/07/24 全球购物
马来西亚在线药房:RoyalePharma
2019/12/01 全球购物
在什么时候需要使用"常引用"
2015/12/31 面试题
什么是SQL Server的确定性函数和不确定性函数
2016/08/04 面试题
音乐器材管理制度
2014/01/31 职场文书
元宵节晚会主持人串词
2014/03/25 职场文书
JavaScript中的宏任务和微任务详情
2021/11/27 Javascript
pytorch中的 .view()函数的用法介绍
2022/03/17 Python