如何交叉编译带有 C 代码的 Python 三方库 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yakumo17
V2EX    Python

如何交叉编译带有 C 代码的 Python 三方库

  •  
  •   yakumo17 2018-03-19 14:27:06 +08:00 4413 次点击
    这是一个创建于 2768 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想往一个 arm linux 上面跑 Python 程序,交叉编译 Python 时就缺过很多像 zlib、openssl 这种库,下载源码修改 gcc、g++这些地址后就能编译进去了。 后来装 Python 库,绝大多数纯 Python 代码复制粘贴也可以了,现在想装 gevent,库里有几个 so 的文件是平台相关的,我觉得应该可以通过 python setup.py build 来编译然后复制过去,找不到从哪改编译器设置了。

    6 条回复    2018-07-12 18:08:11 +08:00
    tempdban
        1
    tempdban  
       2018-03-19 15:55:16 +08:00
    1.rootfs 搞成 debian 的就不会有这些问题。
    2.如果你硬是要编。。。
    export CC=
    export LDSHARED=
    export CFLAGS=
    export LDFLAGS=
    export CROSSBASE= //rootfs 路径,连接动态库。
    python setup.py build
    python bdist_egg
    tempdban
        2
    tempdban  
       2018-03-19 16:02:22 +08:00
    我想了五分钟,你好像不知道 CC 环境变量是干什么的,别折磨自己了
    yakumo17
        3
    yakumo17  
    OP
       2018-03-20 12:16:20 +08:00
    @tempdban 身边没人弄过这个,只能自己来了。其他需要的库我改改 Makefile 都编译了,程序都跑起来了,这是最后一个。

    python setup.py build 会报错,我手动编译这个依赖库会提示 C 编译器错误,换成本机的 gcc-4.8 可以运行,不过就是 x86 版本的了。
    ```
    root@ubuntu:~/gevent-1.2.2/deps/libev# ./configure CC=arm-linux-gnueabihf-gcc --prefix=`pwd`/mylib
    checking for a BSD-compatible install... /usr/bin/install -c
    checking whether build environment is sane... yes
    checking for a thread-safe mkdir -p... /bin/mkdir -p
    checking for gawk... no
    checking for mawk... mawk
    checking whether make sets $(MAKE)... yes
    checking whether make supports nested variables... yes
    checking whether to enable maintainer-specific portions of Makefiles... no
    checking for gcc... arm-linux-gnueabihf-gcc
    checking whether the C compiler works... no
    configure: error: in `/root/gevent-1.2.2/deps/libev':
    configure: error: C compiler cannot create executables
    See `config.log' for more details
    ```
    yakumo17
        4
    yakumo17  
    OP
       2018-03-20 12:27:31 +08:00
    我想着树莓派也是 armv7l,把树莓派上的拷过去也许可以,结果提示缺少_corecffi.so
    https://anaconda.org/search?q=gevent 上面搜到的 gevent 包也是没有这个文件
    yakumo17
        5
    yakumo17  
    OP
       2018-03-20 18:04:01 +08:00
    瞎鼓捣勉强能用了。

    export CC="arm-linux-gnueabihf-gcc -pthread"
    export LDSHARED="${CC} -shared"
    export CROSSBASE=/root/rootfs/rootfs
    先加了这三条,其他不知道填啥了。

    然后 python setup.py build 报错,进入 deps/libev 目录,./configure 提示需要加--host 参数。
    修改_setuplibev.py,_setupares.py ,找到 sh ./configure 所在行,在后面加上--host=arm-linux-gnueabihf

    继续 python setup.py build,可能是配置不对需要将 corecext.cpython-36m-i386-linux-gnu.so 重命名,改成 corecext.so ,然后复制到 arm 系统内就可以用了。

    我看到有提到用 buildroot 之类的工具,似乎是能直接构建?
    NvRay
        6
    NvRay  
       2018-07-12 18:08:11 +08:00
    楼主这个问题解决没有。遇到一样的问题了。快折磨死了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5983 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 03:04 PVG 11:04 LAX 20:04 JFK 23:04
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86