树莓派的CrossCompilie python代码

时间:2020-04-06 23:42:31

标签: python linux gcc raspberry-pi cross-platform

我发现在Ubuntu19.04 64位计算机上安装交叉编译器时遇到问题。我想将运行Debian Stretch的raspberry pi 3模型b +的python代码交叉编译为可执行文件。 我遵循了许多指南,但没有一个起作用。我实际上正在关注https://github.com/Yadoms/yadoms/wiki/Cross-compile-for-raspberry-PI

我遵循了上述书面指南的步骤: -设置环境 -安装交叉编译器 -提升1.64 -Python

在最后一部分(Python)上,它无法执行最后一条指令。

$ CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ AR=arm-linux-gnueabihf-ar RANLIB=arm-linux-gnueabihf-ranlib ./configure --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --build=x86_64-linux-gnu --prefix=$HOME/Desktop/rapsberry/depsBuild/python --disable-ipv6 ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no ac_cv_have_long_long_format=yes --enable-shared

输出:

checking build system type... x86_64-pc-linux-gnu
checking host system type... arm-unknown-linux-gnueabihf
checking for python3.7... python3.7
checking for python interpreter for cross build... python3.7
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for arm-linux-gnueabihf-gcc... arm-linux-gnueabihf-gcc
checking whether the C compiler works... no
configure: error: in `/home/slr/Desktop/raspberry/boost_1_64_0/Python-3.7.5':
configure: error: C compiler cannot create executables
See `config.log' for more details

然后:

$ make HOSTPYTHON=$HOME/Desktop/raspberry/depsBuild/pythonhost/python HOSTPGEN=$HOME/Desktop/raspberry/depsBuild/pythonhost/Parser/pgen BLDSHARED="arm-linux-gnueabihf-gcc -shared" CROSS-COMPILE=arm-linux-gnueabihf- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=arm-linux-gnueabihf 

输出:

 make: *** No targets specified and no makefile found.  Stop.

我需要使用python3。

我真的陷在这个问题中,有人可以提出想法吗?我还尝试了QEMU和Docker(https://raspberrypi.stackexchange.com/questions/109488/building-a-virtual-machine-with-the-img-file-of-the-raspberry-pi-stretch),但它们均未能编译我的目标代码: gcc: internal compiler error 我的代码很长(几千行),而小的代码可以正常工作。感谢您的建议。

1 个答案:

答案 0 :(得分:1)

您使用的工具链或调用Python配置脚本的方式似乎有问题。
无论哪种方式,都无法在没有看到确切设置的情况下进行调试,因此,我将从这里开始。
我正在这里记录类似项目的过程:Raspberry Pi C++ development

工具链

raspberrypi/tools存储库中的工具链非常老。我通常只是使用Crosstool-NG(这也是raspberrypi/tools工具链的构建工具,即IIRC)来构建一个新的工具。
我使用了armv8-rpi3-linux-gnueabihf sample

您当然可以自己构建它,但这可能要花一些时间,因此您还可以下载我从Docker Hub构建的一个(请参阅下文)。

您可以在此处找到有关其构建方式的更多信息:Detailed information about configuring and building toolchains

为构建系统编译Python

为了交叉编译Python模块,您需要两次使用相同版本的Python:一次用于构建系统(正在构建的计算机),一次用于主机系统(正在构建的Raspberry Pi)对于)。

两者都将从源代码编译,以确保它们是完全相同的版本。

构建Python将仅用于交叉编译宿主Python,因此我们将不启用优化和可选模块。
这是我使用的脚本:python-build.sh
您需要OpenSSL,Zlib和libffi才能使pip正常工作。我也从源代码构建了它们,但是您当然可以使用软件包管理器来安装它们(您需要-dev版本)。
同样,您可以找到我使用的here安装脚本。

用于主机系统的交叉编译Python

在为Raspberry Pi交叉编译Python之前,必须交叉编译其依赖项。可以在以下位置找到详细说明:Cross-compiling the dependencies
您可以在我链接到较早版本的GitHub上的同一文件夹中找到脚本,例如python.sh
交叉编译时有一些注意事项,您需要一个pkg-config with the right prefix在交叉编译的sysroot中而不是在构建系统的库文件夹中查找所需的库。调用configure脚本时,还必须指定包含目录和库文件夹。
所有这些操作都由此Dockerfile和同一文件夹中的脚本处理。

Crossenv

交叉编译Python模块的最简单方法是使用Crossenv。可以在GitHub页面的自述文件中找到说明。

设置完所有内容后,您可以运行python setup.py bdist_wheel

示例

作为示例,您可以按照以下步骤使用Cython编译简单的Python模块:

1。从Docker中心获取工具链和交叉编译的Python

这是一张包含交叉编译工具链,本机和交叉编译的Python以及Crossenv的图像。

docker pull tttapa/rpi3-armv8-python-opencv-cross

2。创建要编译的Python文件,然后setup.py进行构建

创建这两个文件:

helloworld.py

print("Hello, World")

setup.py

from setuptools import setup
from Cython.Build import cythonize
from sys import version_info

setup(
    name='helloworld',
    version='0.0.1',
    ext_modules = cythonize("helloworld.py", language_level=version_info[0])
)

3。创建一个构建Python模块的脚本

此脚本将在Docker容器中运行。

build.sh

#!/usr/bin/env bash

set -e
cd /tmp/workdir
source "$HOME/crossenv/bin/activate"
build-pip install cython
cross-pip install wheel
python setup.py bdist_wheel

4。启动Docker容器并构建模块

在创建三个文件的同一文件夹中运行以下命令。

docker run --rm -v "$PWD:/tmp/workdir" \
    tttapa/rpi3-armv8-python-opencv-cross \
    bash "/tmp/workdir/build.sh"

构建完成后,您将找到文件dist/helloworld-0.0.1-cp38-cp38-linux_arm.whl,可以使用pip install helloworld-0.0.1-cp38-cp38-linux_arm.whl将其安装在Raspberry Py上。您还会找到Cython生成的helloworld.c文件。

要运行它,您可能必须将交叉编译的库和Python本身安装到RPi。您可以通过将~/RPi-staging内部(在Docker容器内部)的所有内容复制到Pi的根文件夹/中来实现。