Python - 通过交叉编译将C和Python程序移植到arm机中相关库和文件找不到的问题解决

上一篇的程序通过gcc在x86电脑中可以正确运行,但是移植到arm中时通过arm交叉编译会提示错误。

编译命令为:

main:main.c
    arm-linux-gnueabihf-gcc  -I/usr/include/python2.7/  main.c -o main -L/usr/lib -lpython2.7
clean:
    rm -rf *.o main

第一个错误:提示找不到pyconfig.h文件

In file included from /usr/include/python2.7/Python.h:8:0,
                 from main.c:3:
/usr/include/python2.7/pyconfig.h:13:54: fatal error: arm-linux-gnueabihf/python2.7/pyconfig.h: No such file or directory
 #  include <arm-linux-gnueabihf/python2.7/pyconfig.h>

在/usr/include的pyconfig.h文件中看到是根据不同的平台,pyconfig文件不一致

# elif defined(__ARM_EABI__) && defined(__ARM_PCS_VFP)
#  include <arm-linux-gnueabihf/python2.7/pyconfig.h>
# elif defined(__ARM_EABI__) && !defined(__ARM_PCS_VFP)
#  include <arm-linux-gnueabi/python2.7/pyconfig.h>

查找pyconfig.h所在的目录

sudo find / -name pyconfig.h
/home/duser/mk5/bsp/image/rootfs/usr/include/arm-linux-gnueabihf/python2.7/pyconfig.h

将该目录添加到-I目录中

main:main.c
    arm-linux-gnueabihf-gcc -I/home/duser/mk5/bsp/image/rootfs/usr/include/ -I/usr/include/python2.7/  main.c -o main -L/usr/lib -lpython2.7
clean:
    rm -rf *.o main

2,pyconfig的问题解决后,再次编译又出现以下错误:

/usr/lib/gcc-cross/arm-linux-gnueabihf/4.8/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lpython2.7

通常引起该错误的原因为(1)没有安装对应的库(2)路径链接错误;Python2.7的库可以确定已经安装,所以只能为pythin2.7库的链接路径错误,通过查找找到对应arm机的python2.7库,将其链接到/usr/lib/ld中。

duser@172.20.10.7:/usr/lib$ sudo ln -sv /home/duser/mk5/bsp/image/rootfs/usr/lib/arm-linux-gnueabihf/libpython2.7.so libpython2.7.so

再次运行,提示仍有库缺少,根据上述方法将其链接到ld中;编译通过后将main文件和py_add.py下载到arm机中,运行正常。

root@MK5:/mnt/ubi# ./main 
Function of python called!
('a = ', 123)
('b = ', 321)
('a + b = ', 444)