Raspberry pi的CrossCompilie python代码

问题描述 投票:0回答:1

我发现在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我的代码很长(几千行),而小的代码可以正常工作。感谢您的建议。

python linux gcc raspberry-pi cross-platform
1个回答
0
投票

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

工具链

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

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

您可以在此处找到有关其构建方式的更多信息:armv8-rpi3-linux-gnueabihf

为构建系统编译Python

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

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

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

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

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

Crossenv

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

完成所有设置后,您可以运行pkg-config with the right prefix

示例

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

1。从Docker集线器中提取工具链和交叉编译的Python

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

pkg-config

2。创建要编译的Python文件,然后单击Dockerfile进行构建

创建这两个文件:

helloworld.py

Crossenv

setup.py

python setup.py bdist_wheel

3。创建用于构建Python模块的脚本

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

build.sh

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

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

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

setup.py

构建完成后,您会找到文件print("Hello, World") ,可以使用Raspberry 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]) ) 安装该文件。您还将找到Cython生成的#!/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 文件。

为了能够运行它,您可能必须将交叉编译的库和Python本身安装到RPi。您可以通过将docker run --rm -v "$PWD:/tmp/workdir" \ tttapa/rpi3-armv8-python-opencv-cross \ bash "/tmp/workdir/build.sh" 内部(在Docker容器内部)的所有内容复制到Pi的根文件夹dist/helloworld-0.0.1-cp38-cp38-linux_arm.whl中来进行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.