交叉编译的应用程序不能在Raspberry Pi上运行

时间:2016-07-10 12:36:02

标签: c raspberry-pi cross-platform buildroot

我正在使用Buildroot和Raspberry Pi(运行Raspbian)完成我的第一步。但不知怎的,我似乎对交叉编译做错了。该应用程序是用C编写的最简单的Hello World程序。这就是我所做的:

  1. 已下载并安装了buildroot
  2. make raspberrypi2_defconfig
  3. make toolchain
  4. 然后我写了一个小应用程序和下面的Makefile:

    CROSS_BIN := /home/me/raspi/buildroot-2016.05/output/host/usr/bin
    SYSROOT := /home/me/raspi/buildroot-2016.05/output/host/usr/arm-buildroot-linux-uclibcgnueabihf/sysroot
    PATH := $(CROSS_BIN):$(PATH)
    
    CC := arm-linux-gcc
    CFLAGS := --sysroot=$(SYSROOT)
    
    app: app.c
       $(CC) $(CFLAGS) -o $@ $<
    

    编译应用程序并将其复制到Raspberry。当我试图运行它时,RPI抱怨它无法找到该文件(虽然它可以找到并且可执行文件)。二进制类型对我来说似乎没问题,应该适合CPU:

    pi@raspberrypi:~ $ ./app
    -bash: ./app: No such file or directory
    
    pi@raspberrypi:~ $ ls -l app
    -rwxr-xr-x 1 pi pi 4916 Jul 10 11:07 app
    
    pi@raspberrypi:~ $ file app
    app: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-uClibc.so.0, not stripped
    
    pi@raspberrypi:~ $ lscpu
    Architecture:          armv7l
    Byte Order:            Little Endian
    CPU(s):                4
    On-line CPU(s) list:   0-3
    Thread(s) per core:    1
    Core(s) per socket:    4
    Socket(s):             1
    Model name:            ARMv7 Processor rev 5 (v7l)
    CPU max MHz:           900.0000
    CPU min MHz:           600.0000
    

    有人可以告诉我我做错了什么吗?如果我本地编译应用程序并在开发主机上运行它,它运行没有问题。

2 个答案:

答案 0 :(得分:4)

我的资金已从您的Rasperry pi中遗失/lib/ld-uClibc.so.0。我是对的吗?

好的,这个库是你的动态加载器,它负责在运行时加载你的动态库。它将所需的共享库加载到进程地址空间,并在内存上设置适当的权限(只读,读写和可执行)。

您的交叉编译器需要一个不存在的加载程序,可能是由于RPi上安装的映像与交叉编译环境(您的sysroot)不匹配。

有几种方法可以修复它,让我们首先检查一个有效的二进制文件,尝试file /bin/ls并在此处发布动态加载程序。

例如:

$ file /bin/ls
/bin/ls: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[sha1]=5e052e8de057d379ab51d4af510ad9318fe77b46, stripped

答案 1 :(得分:1)

When I tried to run it, RPI complains that it can't find the file (though it's there and executable for sure).

The shell is complaining that it cannot find a file in order to execute your program, not that it cannot find your file.
If it's installed use the strace command to determine what file cannot be found.
Most likely you have a dynamic library issue, e.g. you built with a uClibc toolchain, but your rootfilesystem has glibc.

Two common solutions:

(A) build your program with static linking (so that it no longer depends on the installed libraries of the target system).

 $(CC) $(CFLAGS) -o -static $@ $< 

(B) rebuild the Buildroot toolchain to match the library that is already installed on your RPi. i.e. instead of a uClibc toolchain, build a glibc toolchain of matching version number.

相关问题