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

2018-03-19 14:27:06 +08:00
 yakumo17

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

3845 次点击
所在节点    Python
6 条回复
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
2018-03-19 16:02:22 +08:00
我想了五分钟,你好像不知道 CC 环境变量是干什么的,别折磨自己了
yakumo17
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
2018-03-20 12:27:31 +08:00
我想着树莓派也是 armv7l,把树莓派上的拷过去也许可以,结果提示缺少_corecffi.so
https://anaconda.org/search?q=gevent 上面搜到的 gevent 包也是没有这个文件
yakumo17
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
2018-07-12 18:08:11 +08:00
楼主这个问题解决没有。遇到一样的问题了。快折磨死了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/439368

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX