使用cython从intel ubuntu到arm交叉编译项目 [英] Using cython to cross compile project from intel ubuntu to arm

查看:623
本文介绍了使用cython从intel ubuntu到arm交叉编译项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的python + cython项目(来自 http:// docs的世界示例)。 cython.org/src/tutorial/cython_tutorial.html )放在我的Ubuntu 16 x86_64上。我可以使用cython为x86_64构建该项目。

I have simple python + cython project (hello world example from http://docs.cython.org/src/tutorial/cython_tutorial.html) on my ubuntu 16 x86_64. I can build this project with cython for x86_64.

如何在不使用真正的armv7开发​​板/ cpu的情况下为ubuntu 15的armv7版本构建项目?

How can I build the project for armv7 version of ubuntu 15 without using real armv7 board/cpu?

我有 arm-linux-gnueabihf-gcc http://packages.ubuntu.com/xenial/devel/gcc-arm-linux-gnueabihf ),它可以为armv7编译简单的C程序。如何更改cython的设置以使用交叉编译器为arm构建共享对象?

I have arm-linux-gnueabihf-gcc (http://packages.ubuntu.com/xenial/devel/gcc-arm-linux-gnueabihf) and it can compile simple C programs for armv7. How can I change settings of cython to use cross compiler for building shared objects for arm?

推荐答案

依赖于体系结构的库和头文件是交叉编译所需。

Architecture dependent libraries and headers files are needed for cross compiling.

测试python3.5-dev软件包和其他软件包是否可以在 dpkg --add-architecture armhf 之后安装和 apt-get update (对sources.list进行一些修改后),结果基本上是这样。

When testing if python3.5-dev package and others could be installed after dpkg --add-architecture armhf and apt-get update (after some modification to sources.list), the result was basically.

python3.5-dev:armhf : Depends: python3.5:armhf (= 3.5.1-10) but it is not going to be installed

apt-get install python3.5:armhf 是行不通的,参见


现有提案允许针对不同体系结构的库和
标头的共同安装,但尚未安装二进制文件。

The existing proposals allow for the co-installation of libraries and headers for different architectures, but not (yet) binaries.

一种可能QEMU和chroot提供了不需要完整虚拟机的解决方案。可以通过 debootstrap 命令创建适合chroot的目录。创建后, schroot 可以授予对该环境的访问权限。

One possible solution that does not require "full" virtual machine is provided by QEMU and chroot. A suitable directory for chroot can be created by debootstrap command. After creation, schroot can give access to that environment.

替换< DIRECTORY> < USER> 在以下命令中:

apt-get install -y debootstrap qemu-user-static binfmt-support schroot
debootstrap --arch=armhf --foreign --include=gcc,g++,python3.5-dev xenial <DIRECTORY>
cp /usr/bin/qemu-arm-static <DIRECTORY>/usr/bin
chroot <DIRECTORY>
/debootstrap/debootstrap --second-stage
echo "deb http://ports.ubuntu.com/ubuntu-ports xenial universe" >> /etc/apt/sources.list
echo "deb http://ports.ubuntu.com/ubuntu-ports xenial multiverse" >> /etc/apt/sources.list
apt-get update
apt-get install -y cython cython3
exit
cat <<END > /etc/schroot/chroot.d/xenial-armhf
[xenial-armhf]
description=Ubuntu xenial armhf
type=directory
directory=/home/xenial-armhf
groups=sbuild,root
root-groups=sbuild,root
users=root,<USER>
END

环境应该可由

schroot -c chroot:xenial-armhf

,对于root用户会话(用户必须位于root-groups中列出的组中),

and for root user session (the user must be in a group listed in root-groups) ,

schroot -c chroot:xenial-armhf -u root

之后,还可以交叉编译cython模块:

After this, it is also possible to cross compile a cython module:

hello.pyx:

hello.pyx:

print("hello world")

编译( python3.5-config --cflags 和chroot中的 python3.5-config --libs 作为选项,请注意 -fPIC ):

compiling (python3.5-config --cflags and python3.5-config --libs in chroot for options, note -fPIC):

cython hello.pyx
arm-linux-gnueabihf-gcc --sysroot <DIRECTORY> -I/usr/include/python3.5m -I/usr/include/python3.5m  -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -c hello.c
arm-linux-gnueabihf-gcc --shared --sysroot <DIRECTORY> -lpython3.5m -lpthread -ldl  -lutil -lm hello.o -o hello.so

然后可以测试模块

schroot -c chroot:xenial-armhf
python3
import hello

基于cython的交叉编译python模块也可以工作。使用setup.py

Cross compiling cython based python modules may also work. With setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

import os

os.environ['CC'] = 'arm-linux-gnueabihf-gcc'
os.environ['LDSHARED'] = 'arm-linux-gnueabihf-gcc -shared'
sysroot_args=['--sysroot', '/path/to/xenial-armhf']

setup(cmdclass = {'build_ext': build_ext},
      ext_modules= [ Extension("hello", ["hello.pyx"],
                                extra_compile_args=sysroot_args,
                                extra_link_args=sysroot_args) ])

用这种方法可以构建一个简单的 hello world 模块。该模块的文件名错误,在这种情况下为 hello.cpython-35m-x86_64-linux-gnu.so 。将其重命名为 hello.so 后,可以将其导入。

Building a simple hello world module was possible this way. The file name for the module was wrong, in this case it was hello.cpython-35m-x86_64-linux-gnu.so. After renaming it as hello.so it was possible to import it.

这篇关于使用cython从intel ubuntu到arm交叉编译项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆