【问题标题】:gcc - compile for another machine does not workgcc - 为另一台机器编译不起作用
【发布时间】:2013-04-04 15:54:16
【问题描述】:

我遇到以下问题:

  • 我有旧系统 - ARM CPU。
  • 在旧系统上,我没有开发库,也没有 GCC。
  • 在旧系统上我没有足够的资源(主要是 RAM 和 SWAP 是不可能的)来编译。

我需要编译我有源代码的非常简单的程序。当我在另一台也有 ARM CPU(更新一点)的机器上使用以下 GCC 命令编译它时:

gcc -mcpu=arm920t -march=armv4t -o app app.c

(-mcpu 和 -march 设置为旧系统的 CPU)

当我将此编译文件复制到旧系统时,使其可执行并尝试运行它,我收到以下消息:

$ ./app
-sh: ./app: No such file or directory
$ ls -lah
total 237K
drwxr-xr-x  2 root    root    0 Apr  4 07:35 .
drwxr-xr-x  4 root    root    0 Dec 31  1969 ..
-rwxr-xr-x  1 root    root 108K Mar 14 09:23 app
$

路径没有问题,分区使用 noexec 选项挂载。当我将任何系统(例如 cat)文件复制到同一目录时,我可以毫无问题地启动它。

这是我编译的应用程序(第一个结果)和已经在系统上的应用程序(第二个结果)之间的区别:

$ readelf -h app
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0xa278
  Start of program headers:          52 (bytes into file)
  Start of section headers:          100104 (bytes into file)
  Flags:                             0x5000002, has entry point, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         10
  Size of section headers:           40 (bytes)
  Number of section headers:         38
  Section header string table index: 35
$

还有:

$ readelf -h cat
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 61 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            ARM
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x8c34
  Start of program headers:          52 (bytes into file)
  Start of section headers:          15220 (bytes into file)
  Flags:                             0x2, has entry point, GNU EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         6
  Size of section headers:           40 (bytes)
  Number of section headers:         24
  Section header string table index: 23
$

file cat app的输出:

app: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped
cat: ELF 32-bit LSB executable, ARM, version 1, dynamically linked (uses shared libs), for GNU/Linux 2.2.0, stripped

我不确定这里有什么问题,因为即使应用程序编译错误,我也希望收到与“没有这样的文件或目录”不同的错误消息。

【问题讨论】:

  • “Version5 ABI”与“GNU EABI”可能会造成一些麻烦。 file cat app 有什么要说的?如果答案不相同,则需要找出不同之处。
  • app:ELF 32 位 LSB 可执行文件,ARM,版本 1 (SYSV),动态链接(使用共享库),用于 GNU/Linux 2.6.16,未剥离 --- cat: ELF 32 -bit LSB 可执行文件,ARM,版本 1,动态链接(使用共享库),对于 GNU/Linux 2.2.0,已剥离我可以看到 3 个不同之处: - 我的应用程序没有剥离(据我所知这不是问题,即使我喜欢它,我也有同样的问题。)。 - 我的应用程序具有更新的 GNU/Linux 专业化。 - 对于我的应用程序,提到了 SYSV,我不知道那是什么,没有它我如何编译应用程序:(
  • 目标机器似乎没有 GNU/Linux 专业化所需的支持。除非您可以在相同类型的目标上找到工作二进制文件,否则您很不走运。要么升级对目标的支持,要么只使用目标已经支持的..

标签: gcc cross-compiling legacy


【解决方案1】:

这个问题主要是由于共享库libc问题。当你在支持gcc的arm机器上编译程序时,使用的库(libc)与你的目标不同。所以首先在主机和目标上检查你的库。 要交叉检查上面的评论,你试试这个。

gcc -static -o app app.c

现在将您的应用程序可执行文件复制到您的目标。在目标上执行,它将毫无问题地执行。 检查上面静态编译的代码。采用 文件应用 或者 ldd 应用程序。

如果你不想静态编译,那么从目标 rootfs 的 /lib 复制你的库 编译时提供目标库而不是主机库的库路径。这可以通过

LDFLAGS=-L/<path-to-target-lib>  gcc  -o app app.c

【讨论】:

  • 单独复制库并不能解决目标 ABI 差异。这取决于 GCC 的配置方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多