用cython交叉编译intel-ubuntu到arm的项目

2024-10-01 13:24:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的ubuntu16x86u64上有一个简单的python+cython项目(来自http://docs.cython.org/src/tutorial/cython_tutorial.html的hello world示例)。我可以用cython为x86\u64构建这个项目。在

我如何在不使用真正的armv7板/cpu的情况下为ubuntu15的armv7版本构建项目?在

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


Tags: 项目orgsrchttpdocshellolinuxhtml
1条回答
网友
1楼 · 发布于 2024-10-01 13:24:00

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

当测试python3.5-dev包和其他包是否可以安装在dpkg add-architecture armhf和{}(在对来源.列表),结果基本上是。在

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是不起作用的,see

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

QEMU和chroot提供了一种不需要“完整”虚拟机的解决方案。可以通过debootstrap命令为chroot创建一个合适的目录。{cd5>创建后可以访问环境。在

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

^{pr2}$

环境应通过

schroot -c chroot:xenial-armhf

对于根用户会话(用户必须位于根组中列出的组中)

schroot -c chroot:xenial-armhf -u root

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

在你好.pyx公司名称:

print("hello world")

编译(python3.5-config cflags和{}在chroot中用于选项,请注意-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模块也可以工作。与设置.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之后,就可以导入它了。在

相关问题 更多 >