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 · 3831 次点击
    这是一个创建于 2229 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想往一个 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   ·   我们的愿景   ·   实用小工具   ·   5580 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 02:40 · PVG 10:40 · LAX 19:40 · JFK 22:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.