【发布时间】: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 & 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