python扩展模块交叉编译

2 篇文章 0 订阅
1 篇文章 0 订阅

搞个啥

这篇主要是最近在工作上,需要在板子上跑python,有一些扩展比较麻烦,比方说spidev啥的,单纯的使用

		pip install spidev	

是不行的,这个会报错。如下:

	Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting spidev
  Downloading http://mirrors.aliyun.com/pypi/packages/fb/14/4c2e1640f0cb04862c76d9d76ed7c945b0f67876e503ac02f7f675fe86a0/spidev-3.4.tar.gz
Installing collected packages: spidev
  Running setup.py install for spidev ... error
    ERROR: Complete output from command /usr/bin/python -u -c 'import setuptools, tokenize;__file__='"'"'/var/volatile/tmp/pip-install-JQUClU/spidev/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /var/volatile/tmp/pip-record-OgQGW6/install-record.txt --single-version-externally-managed --compile:
    ERROR: running install
    running build
    running build_ext
    building 'spidev' extension
    creating build
    creating build/temp.linux-armv7l-2.7
    arm-poky-linux-gnueabi-gcc -march=armv7ve -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a7 -fno-strict-aliasing -O2 -pipe -g -feliminate-unused-debug-types -fdebug-prefix-map=/home/zhuanghj/Downloads/yocto/fsl-release-bsp/build/tmp/work/cortexa7hf-neon-poky-linux-gnueabi/python/2.7.11-r1=/usr/src/debug/python/2.7.11-r1 -fdebug-prefix-map=/home/zhuanghj/Downloads/yocto/fsl-release-bsp/build/tmp/sysroots/x86_64-linux= -fdebug-prefix-map=/home/zhuanghj/Downloads/yocto/fsl-release-bsp/build/tmp/sysroots/imx6ulevk= -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c spidev_module.c -o build/temp.linux-armv7l-2.7/spidev_module.o
    unable to execute 'arm-poky-linux-gnueabi-gcc': No such file or directory
    error: command 'arm-poky-linux-gnueabi-gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command "/usr/bin/python -u -c 'import setuptools, tokenize;__file__='"'"'/var/volatile/tmp/pip-install-JQUClU/spidev/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /var/volatile/tmp/pip-record-OgQGW6/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /var/volatile/tmp/pip-install-JQUClU/spidev/

这就有点蛋疼了。
主要问题是在板子上没有交叉编译器,但是有一些C的文件需要编译成库供调用(性能原因??)。但是这样的话就比较麻烦。要么就是找一个for arm的编译器(我自己是在 arm上使用的)。后来又找到了一个方案:把python扩展编译为egg。然后再放到板子上进行安装。

编译成egg怎么搞。。。
以下以spidev3.0.4为例:

zhuanghj@ubuntu:~/Downloads/spidev-3.4$ ls
CHANGELOG.md  PKG-INFO  README.md  setup.cfg  setup.py  spidev_module.c

按照网上一些直接运行

python setup.py bdist_egg

会出现报错。

python setup.py bdist_egg
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

找了一会儿,然后发现需要在setpu.py文件中加入一行

from setuptools import setup, find_packages

而且需要放在

from distutils.core import setup, Extension

上面否则会出现报错。

error: each element of 'ext_modules' option must be an Extension instance or 2-tuple

完成这一步之后,我们就可以进行编译了,但是我们是要编译给arm平台。所以要设置一些东西
大致是一些arch CC CXX之类的一些东西。在此不进行叙述。
然后运行:

python setup.py bdist_egg

还是出现报错:

/usr/include/python2.7/pyconfig.h:24:12: fatal error: arm-linux-gnueabihf/python2.7/pyconfig.h: No such file or directory
 #  include <arm-linux-gnueabihf/python2.7/pyconfig.h>

缺少python头文件。那就添加

export CFLAGS="-I/home/zhuanghj/Downloads/yocto/fsl-release-bsp/build/tmp/sysroots/imx6ulevk/usr/include/python2.7 $CFLAGS"

还是报错:

x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -I/home/zhuanghj/Downloads/yocto/fsl-release-bsp/build/tmp/sysroots/imx6ulevk/usr/include/python2.7 -O2 -pipe -g -feliminate-unused-debug-types build/temp.linux-x86_64-2.7/spidev_module.o -o build/lib.linux-x86_64-2.7/spidev.so
/usr/bin/ld: build/temp.linux-x86_64-2.7/spidev_module.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: build/temp.linux-x86_64-2.7/spidev_module.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: build/temp.linux-x86_64-2.7/spidev_module.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: build/temp.linux-x86_64-2.7/spidev_module.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: build/temp.linux-x86_64-2.7/spidev_module.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: build/temp.linux-x86_64-2.7/spidev_module.o: Relocations in generic ELF (EM: 40)
build/temp.linux-x86_64-2.7/spidev_module.o: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

不知道为什么没有调用已经指定了的交叉编译器的ld。
但是最终我还是通过手动编译编译出了spidev.so
然后在进行

python setup.py bdist_egg

编译通过。

running bdist_egg
running egg_info
writing spidev.egg-info/PKG-INFO
writing top-level names to spidev.egg-info/top_level.txt
writing dependency_links to spidev.egg-info/dependency_links.txt
reading manifest file 'spidev.egg-info/SOURCES.txt'
writing manifest file 'spidev.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-2.7/spidev.so -> build/bdist.linux-x86_64/egg
creating stub loader for spidev.so
byte-compiling build/bdist.linux-x86_64/egg/spidev.py to spidev.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying spidev.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying spidev.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying spidev.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying spidev.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
creating 'dist/spidev-3.4-py2.7-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)

还是有点偷懒,应该把ARCH从x86_64改过来的,但是文件已经编译出来了,我也就没有管了。
下次如果有比较复杂的扩展可以试试修改。

到这里就结束。谢谢看完。如果有更好的方法或者有什么问题,欢迎评论,谢谢。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值