交叉编译的共享库不会链接到libstdc ++。所以

时间:2014-11-27 19:59:20

标签: python c++ arm shared-libraries

我试图将我的python c模块交叉编译为ARM(beaglebone-black具体)。一切似乎都没问题,但我无法将其正确导入到我所假设的链接器运行时问题中。

root@beaglebone:~# python3
Python 3.4.2 (default, Oct  8 2014, 14:38:51)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mypackage
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/local/lib/python3.4/dist-packages/mypackage.cpython-34m.so: cannot open shared object file: No such file or directory

root@beaglebone:~# ldd /usr/local/lib/python3.4/dist-packages/mypackage.cpython-34m.so
        libstdc++.so.6 => not found
        libgcc_s.so.1 => /lib/arm-linux-gnueabi/libgcc_s.so.1 (0xb6e88000)
        libc.so.6 => /lib/arm-linux-gnueabi/libc.so.6 (0xb6d42000)
        /lib/ld-linux.so.3 (0xb6ef9000)

root@beaglebone:~# ls -l /usr/local/lib/python3.4/dist-packages/
-rw-r--r-- 1 debian admin    244 Nov 27 12:19 mypackage.egg-info
-rwxr-xr-x 1 debian admin 698298 Nov 27 12:19 mypackage.cpython-34m.so
-rwxrwxrwx 1 root   root  641216 Nov 27 12:29 libstdc++.so.6

root@beaglebone:~# ldconfig -p |grep libstd*
        libstdc++.so.6 (libc6,hard-float) => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6

root@beaglebone:~# file /usr/local/lib/python3.4/dist-packages/mypackage.cpython-34m.so
/usr/local/lib/python3.4/dist-packages/mypackage.cpython-34m.so: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked, BuildID[sha1]=0x9a03edac27af0002b195cc47ddc5396f80cb1366, not stripped

root@beaglebone:~# file /usr/local/lib/python3.4/dist-packages/libstdc++.so.6
/usr/local/lib/python3.4/dist-packages/libstdc++.so.6: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked, BuildID[sha1]=0x8aed4dbc737c88ee5428052996d7657298dd0952, stripped

编辑回答以下问题:

root@beaglebone:~# readelf -A /usr/local/lib/python3.4/dist-packages/mypackage.cpython-34m.so
Attribute Section: aeabi
File Attributes
  Tag_CPU_name: "5T"
  Tag_CPU_arch: v5T
  Tag_ARM_ISA_use: Yes
  Tag_THUMB_ISA_use: Thumb-1
  Tag_ABI_PCS_wchar_t: 4
  Tag_ABI_FP_denormal: Needed
  Tag_ABI_FP_exceptions: Needed
  Tag_ABI_FP_number_model: IEEE 754
  Tag_ABI_align_needed: 8-byte
  Tag_ABI_align_preserved: 8-byte, except leaf SP
  Tag_ABI_enum_size: int

root@beaglebone:~# readelf -A /usr/lib/arm-linux-gnueabihf/libstdc++.so.6
Attribute Section: aeabi
File Attributes
  Tag_CPU_name: "7-A"
  Tag_CPU_arch: v7
  Tag_CPU_arch_profile: Application
  Tag_ARM_ISA_use: Yes
  Tag_THUMB_ISA_use: Thumb-2
  Tag_FP_arch: VFPv3-D16
  Tag_ABI_PCS_wchar_t: 4
  Tag_ABI_FP_denormal: Needed
  Tag_ABI_FP_exceptions: Needed
  Tag_ABI_FP_number_model: IEEE 754
  Tag_ABI_align_needed: 8-byte
  Tag_ABI_align_preserved: 8-byte, except leaf SP
  Tag_ABI_enum_size: int
  Tag_ABI_HardFP_use: SP and DP
  Tag_ABI_VFP_args: VFP registers
  Tag_ABI_optimization_goals: Aggressive Speed
  Tag_CPU_unaligned_access: v6
  Tag_DIV_use: Not allowed

root@beaglebone:~# readelf -A /lib/arm-linux-gnueabi/libc.so.6
Attribute Section: aeabi
File Attributes
  Tag_CPU_name: "4T"
  Tag_CPU_arch: v4T
  Tag_ARM_ISA_use: Yes
  Tag_THUMB_ISA_use: Thumb-1
  Tag_ABI_PCS_wchar_t: 4
  Tag_ABI_FP_rounding: Needed
  Tag_ABI_FP_denormal: Needed
  Tag_ABI_FP_exceptions: Needed
  Tag_ABI_FP_number_model: IEEE 754
  Tag_ABI_align_needed: 8-byte
  Tag_ABI_align_preserved: 8-byte, except leaf SP
  Tag_ABI_enum_size: int

1 个答案:

答案 0 :(得分:0)

您的系统似乎是硬浮点。 hardfp和softfp之间的区别很好地总结了here

-mfloat-abi=name
    Specifies which floating-point ABI to use. Permissible values are: ‘soft’, ‘softfp’
    and ‘hard’.

    Specifying ‘soft’ causes GCC to generate output containing library calls for 
    floating-point operations. ‘softfp’ allows the generation of code using hardware
    floating-point instructions, but still uses the soft-float calling conventions.
   ‘hard’ allows generation of floating-point instructions and uses FPU-specific
    calling conventions.

    The default depends on the specific target configuration. Note that the hard-float
    and soft-float ABIs are not link-compatible; you must compile your entire program
    with the same ABI, and link with a compatible set of libraries.

最后一段说明通常hardfp和softfp不兼容链接。当您执行python解释器(硬浮点)时,动态链接器会尝试加载{float}共享对象mypackage.cpython-34m.so并失败。

执行ldd:

ldd /usr/local/lib/python3.4/dist-packages/mypackage.cpython-34m.so

在softfp共享对象上,结果如下:

libstdc++.so.6 => not found
libgcc_s.so.1 => /lib/arm-linux-gnueabi/libgcc_s.so.1 (0xb6e88000)
libc.so.6 => /lib/arm-linux-gnueabi/libc.so.6 (0xb6d42000)
/lib/ld-linux.so.3 (0xb6ef9000)

链接器中可见的libstdc++.so.6包括路径(如ldconfig所示)是/usr/lib/arm-linux-gnueabihf/libstdc++.so.6,它是硬浮点因此不兼容。如果/usr/local/lib/python3.4/dist-packages/libstdc++.so.6是软浮动(我无法判断您的尝试),我仍然希望以下导出可以解决加载libstdc++.so.6的问题。

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/python3.4/dist-packages/

/lib/arm-linux-gnueabi/libc.so.6是软浮动,它正确加载。为什么你的硬浮动系统上有一个软浮动libc老实说,我无法分辨。

无论如何,要解决python解释器的初始问题,你需要一个硬浮点mypackage.cpython-34m.so,你需要能够用使用相同ABI编译的库来解决依赖关系。

正如您已经看到的,通过使用gcc/g++arm-linux-gnueabihf-gcc-4.7)的硬浮点版本,您设法修复了运行时链接问题。我再次不确定为什么简单地使用-mfloat-abi=hard不起作用。它本应该产生一个硬浮点二进制文件,应该已经足够了。

相关问题