我正在尝试使用cython和cython_freeze交叉编译Python应用程序。 主机是UBUNTU 17.04 x64。目标是Raspberry Pi UBUNTU MATE 16.04(gnueabihf)。
编译有效,但在运行时,当在应用程序中导入ctypes时,会发生ImportError:
Traceback (most recent call last):
File "cttest.py", line 1, in init cttest
File "/usr/local/lib/python3.6/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ImportError: /usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-armlinux-gnueabihf.so: undefined symbol: _PyUnicode_AsWideCharString
什么&#39;错?
(如果对目标进行编译,一切正常)。
编译:
python3.6 cython_freeze -o ctmain.c cttest
arm-linux-gnueabihf-gcc -pthread -fPIC -O2 -Wall -Wextra -fno-strict-aliasing -fwrapv -I/root/eabi-chroot/usr/local/include/python3.6m -c -o ctmain.o ctmain.c
python3.6 -m cython cttest
arm-linux-gnueabihf-gcc -pthread -fPIC -O2 -Wall -Wextra -fno-strict-aliasing -fwrapv -I/root/eabi-chroot/usr/local/include/python3.6m -c -o cttest.o cttest.c
arm-linux-gnueabihf-gcc -Xlinker -export-dynamic -L/root/eabi-chroot/usr/local/lib/python3.6/config-3.6m-arm-linux-gnueabihf ctmain.o cttest.o -lpython3.6m -lm -lpthread -ldl -lutil -o ctmain
Python文件(cttest.py):
import ctypes
def main():
print("OK")
if __name__=="__main__":
main()
注意:实际环境要复杂得多(ctypes导入的是各种第三方库)。
交叉编译的加载程序调试输出的摘录:
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=./ctmain [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/usr/lib/arm-linux-gnueabihf/libarmmem.so [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/arm-linux-gnueabihf/libm.so.6 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/arm-linux-gnueabihf/libpthread.so.0 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/arm-linux-gnueabihf/libdl.so.2 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/arm-linux-gnueabihf/libutil.so.1 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/arm-linux-gnueabihf/libc.so.6 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/ld-linux-armhf.so.3 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-arm-linux-gnueabihf.so [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/arm-linux-gnueabihf/libgcc_s.so.1 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/arm-linux-gnueabihf/libpthread.so.0 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/arm-linux-gnueabihf/libc.so.6 [0]
4154: symbol=_PyUnicode_AsWideCharString; lookup in file=/lib/ld-linux-armhf.so.3 [0]
4154: /usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-arm-linux-gnueabihf.so: error: symbol lookup error: undefined symbol: _PyUnicode_AsWideCharString (fatal)
4154:
4154: file=/usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-arm-linux-gnueabihf.so [0]; destroying link map
4154:
4154: file=/lib/arm-linux-gnueabihf/libgcc_s.so.1 [0]; destroying link map
本地编译的loder调试输出的摘录:
4124: symbol=PyUnicode_AsWideChar; lookup in file=./cttest [0]
4124: symbol=_ctypes_simple_instance; lookup in file=./cttest [0]
4124: symbol=_ctypes_simple_instance; lookup in file=/usr/lib/arm-linux-gnueabihf/libarmmem.so [0]
4124: symbol=_ctypes_simple_instance; lookup in file=/lib/arm-linux-gnueabihf/libm.so.6 [0]
4124: symbol=_ctypes_simple_instance; lookup in file=/lib/arm-linux-gnueabihf/libpthread.so.0 [0]
4124: symbol=_ctypes_simple_instance; lookup in file=/lib/arm-linux-gnueabihf/libdl.so.2 [0]
4124: symbol=_ctypes_simple_instance; lookup in file=/lib/arm-linux-gnueabihf/libutil.so.1 [0]
4124: symbol=_ctypes_simple_instance; lookup in file=/lib/arm-linux-gnueabihf/libc.so.6 [0]
4124: symbol=_ctypes_simple_instance; lookup in file=/lib/ld-linux-armhf.so.3 [0]
4124: symbol=_ctypes_simple_instance; lookup in file=/usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-arm-linux-gnueabihf.so [0]