基于 ARM 的嵌入式 Linux 系统的交叉编译 [英] Cross-Compiling for an embedded ARM-based Linux system

查看:32
本文介绍了基于 ARM 的嵌入式 Linux 系统的交叉编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为嵌入式(自定义)基于 ARM 的 Linux 系统编译一些 C 代码.我使用名为 arm-linux-gnueabi-gcc-4.4 的交叉编译器设置了 Ubuntu VM,因为它看起来像我需要的.现在,当我用这个 gcc 编译我的代码时,它会产生一个像这样的二进制文件:

I try to compile some C code for an embedded (custom) ARM-based Linux system. I set up an Ubuntu VM with a cross-compiler named arm-linux-gnueabi-gcc-4.4 because it looked like what I needed. Now when I compile my code with this gcc, it produces a binary like this:

$ file test1
test1: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked
(uses shared libs), for GNU/Linux 2.6.31,
BuildID[sha1]=0x51b8d560584735be87adbfb60008d33b11fe5f07, not stripped

当我尝试在嵌入式 Linux 上运行这个二进制文件时,我得到

When I try to run this binary on the embedded Linux, I get

$ ./test1
-sh: ./test1: not found

权限就足够了.我只能想象二进制格式有问题,所以我查看了一些工作二进制文件作为参考:

Permissions are sufficient. I can only imagine that something's wrong with the binary format, so I looked at some working binary as reference:

$ file referenceBinary
referenceBinary: ELF 32-bit LSB executable, ARM, version 1, dynamically linked
(uses shared libs), stripped

我看到存在一些差异,但我不知道我究竟需要修复什么以及如何修复.有人能解释一下哪个差异是关键的吗?

I see that there are some differences, but I do not have the knowledge to derive what exactly I need to fix and how I can fix that. Can someone explain which difference is critical?

我看到的另一件事是依赖项:

Another thing I looked at are the dependencies:

$ ldd test1
    libc.so.6 => not found (0x00000000)
    /lib/ld-linux.so.3 => /lib/ld-linux.so.3 (0x00000000)

(有趣的是,这适用于目标系统,尽管它无法执行二进制文件.)嵌入式系统只有一个 libc.so.0 可用.我想我需要告诉编译器我想要链接的 libc 版本,但据我所知,gcc 只是链接它附带的版本,这是正确的吗?我该怎么办?

(Interestingly, this works on the target system although it cannot execute the binary.) The embedded system only has a libc.so.0 available. I guess I need to tell the compiler the libc version I want to link against, but as I understand it, gcc just links against the version it comes with, is this correct? What can I do about it?

这是我使用的 Makefile:

Here's the Makefile I use:

CC=/usr/bin/arm-linux-gnueabi-gcc-4.4
STRIP=/usr/bin/arm-linux-gnueabi-strip          
CFLAGS=-I/usr/arm-linux-gnueabi/include             
LDFLAGS=-nostdlib
LDLIBS=../libc.so.0

SRCS=test1.c
OBJS=$(subst .c,.o,$(SRCS))

all: test1

test1: $(OBJS)
    $(CC) $(LDFLAGS) -o main $(OBJS) $(LDLIBS)
    $(STRIP) main

depend: .depend

.depend: $(SRCS)
    rm -f ./.depend
    $(CC) $(CFLAGS) -MM $^>>./.depend;

clean:
    rm -f $(OBJS)

include .depend

推荐答案

你可能应该做的是在嵌入式系统上安装 libc6.阅读这个话题一个类似的问题.帖子 #5 中的解决方案是安装:

What you should probably do is to install libc6 on the embedded system. Read this thread about a similar problem. The solution in post #5 was to install:

libc6_2.3.6.ds1-13etch9_arm.deb
linux-kernel-headers_2.6.18-7_arm.deb
libc6-dev_2.3.6.ds1-13etch9_arm.deb

您的另一个选择是从嵌入式系统获取 libc 到您的 VM,然后将其传递给 gcc 链接器并使用 -static选项.

Your other option is to get the libc from the embedded system onto your VM and then pass it to the gcc linker and use the -static option.

上面的帖子中也提到了这个解决方案.在此处阅读有关静态链接的更多信息.

This solution was also mentioned in the above thread. Read more about static linking here.

其他可以尝试的事情:

这个帖子中,他们建议删除-mabi=apcs-gnu 如果您正在使用 makefile 中的标志.

In this thread they suggest removing the -mabi=apcs-gnu flag from your makefile if you're using one.

这个如果您从命令行编译,文章 建议使用 feedint gcc -nostdlib 标志.

This article suggests feedint gcc the -nostdlib flag if you're compiling from the command line.

或者您可以切换到使用 arm-none-eabi-gcc 编译器.可以在此处此处.

Or you could switch to using the arm-none-eabi-gcc compiler. References on this can be found here and here.

这篇关于基于 ARM 的嵌入式 Linux 系统的交叉编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆