【问题标题】:CrossCompilie python code for raspberry pi树莓派的 CrossCompilie python 代码
【发布时间】:2020-04-06 23:42:31
【问题描述】:

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

我按照上述书面指南的步骤进行操作: - 设置环境 - 安装交叉编译器 - 提升 1.64 - 蟒蛇

在最后一部分(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 我的代码很长(几千行),而小代码可以成功运行。感谢您的建议。

【问题讨论】:

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


    【解决方案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 中而不是在构建系统的库文件夹中查找所需的库。您还必须在调用配置脚本时指定包含目录和库文件夹。
    所有这一切都由这个Dockerfile 和同一文件夹中的脚本处理。

    交叉环境

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

    一切都设置好后,你可以运行python setup.py bdist_wheel

    示例

    例如,您可以按照以下步骤使用 Cython 编译一个简单的 Python 模块:

    1。从 Docker hub 拉取工具链和交叉编译的 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,您可以在 Raspberry Py 上使用 pip install helloworld-0.0.1-cp38-cp38-linux_arm.whl 安装该文件。您还可以找到 Cython 生成的 helloworld.c 文件。

    为了能够运行它,您可能必须将交叉编译的库和 Python 本身安装到 RPi。您可以通过将 ~/RPi-staging(Docker 容器内)中的所有内容复制到 Pi 的根文件夹 / 来完成此操作。

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多