【问题标题】:How to compile C-code on a 64-bit Linux system for an ARM 32-bit executable如何在 64 位 Linux 系统上为 ARM 32 位可执行文件编译 C 代码
【发布时间】:2021-11-21 09:45:53
【问题描述】:

我正在使用 Linux Ubuntu 64 位并编译 C 代码以在 Linux Armbian 32 位系统中运行。我正在使用:

arm-linux-gnueabi-gcc -c test.c -o test.o
arm-linux-gnueabi-g++ -o test test.o -static

当我在 Ubuntu 上测试代码时,我使用 gcc/g++,但是当我完成后,我编译了一个带有上述“arm..”和 scp“test”到 Armbian 32 系统的链接。

这适用于简单的“Hello World”代码,只要“-static”就在那里。如果我不使用“-static”,即使是简单的可执行文件也无法在 Armbian 32 系统上运行。

但如果我包含使用“gethostbyname”的代码,我会得到 警告,生成的可执行文件将无法在 Armbian 32 位上运行。 我的编译和链接应该是什么,以便我可以在 Ubuntu 64 位中编译并将可执行文件复制到 Armbian 32 位?

我找到的唯一解决方案是在 Armbian 上进行所有编译和链接,但我想避免这种情况,因为我的所有工具都不在那里。 这是代码

#include <stdio.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
struct hostent *lh = gethostbyname("localhost");
if (lh)
    puts(lh->h_name);
else
    herror("gethostbyname");
return 0;
}

链接时:

arm-linux-gnueabi-g++ -o test test.o -static -lrt

我收到警告:

test.c:(.text+0x18): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the libc version used for linking

在 32 位 Armbian 上运行时出现错误:

gethostbyname: Resolver internal error

当我链接到:

arm-linux-gnueabi-g++ -o test test.o -lrt

我没有收到任何警告。但是在 32 位 Armbian 上运行它时出现错误:

unable to execute ./test: No such file or directory

【问题讨论】:

  • stackoverflow.com/questions/15165306/… hat should my compile &amp; link be so that I could compile in Ubuntu 64-bit and copy the executable to the Armbian 32-bit? 所以删除-static?
  • 如果删除“-static”我得到“找不到文件”。问题(我相信)是当从 Ubuntu 链接时“arm-linux-gnueabi-g++”添加了在 32 位 Armbian 中找不到的库函数的动态地址。
  • I'll get get warning: Using 'gethostbyname' get "File not found". 什么文件?请将完整的逐字错误消息发布到问题中。
  • 我编辑了我的问题并添加了所有消息和来源

标签: linux gcc arm ubuntu-18.04 gethostbyname


【解决方案1】:

当您尝试运行一个非常明显存在的程序时,“没有这样的文件或目录”错误是解释器(也称为动态链接器)设置不正确的证据。在你的手臂机器上,做:

 file ./test
 file /bin/ls (or any other dynamically linked executable that works)

并比较输出。

使用 GNU ld--dynamic-linker= 选项来设置正确的动态链接器。

不要尝试静态链接glibc,它不起作用(或者它不像大多数人期望的那样起作用)。

【讨论】:

  • 我没怎么用过ld。 “--dynamic-linker=”是否可用于 g++?在这个简单的案例中,完整的链接命令是什么? ld ... ?
  • 不要直接使用ld。 gcc 可以使用-Wl, 将任何选项传递给 ld,例如g++ -Wl,--dynamic-linker=....
  • "--dynamic-linker=" 之后的内容
  • 显然是动态链接器的文件名。 file /bin/ls 说什么?
  • 也许可以试试arm-linux-gnueabi-g++ -o test test.o -lrt -Wl,--dynamic-linker=/lib/ld-linux-armhf.so.3。我没有手臂系统来检查这个。
猜你喜欢
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 2013-09-20
相关资源
最近更新 更多