首页 > 解决方案 > 使用最新的 cross_compiler 运行简单的 hello world 程序时得到非法指令

问题描述

每个人。我使用最新的 cross_compiler(在 Ubuntu 18.04 上由 sudo apt install gcc-arm-linux-gnueabi 安装,版本为 7.3)编译了一个简单的“hello world”c 程序

#include <stdio.h>

int main()
{
    printf("Hello World!\r\n");
    return 0;
}

使用下面的命令编译代码,我使用静态链接

arm-linux-gnueabi-gcc -o hello hello.c -static

然后我首先在我的树莓派 pi0w 上运行它,它运行得非常好之后,我在我的 s3c2440 板上运行它,它是 ARM9 和非常旧的内核。但是,它运行不正确。

# ./hello
Illegal instruction

不知道是什么原因,怎么解决。然后我用cross_compiler 3.4.5版本再次编译,这次程序运行顺利。但是在树莓派上无法正常运行,运行程序后,什么也没发生。 “你好世界!” 没有出现。

我用file命令检查了这两个程序(不知道有没有帮助):最新编译器编译的程序是:

hello: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=6b0a4b6cb4bf672d4cf1fbd171ec62a96e622d69, not stripped

旧编译器是:

hello_old: ELF 32-bit LSB executable, ARM, version 1 (ARM), statically linked, for GNU/Linux 2.4.3, with debug_info, not stripped

最新的 arm-linux-gnueabi-gcc 是

$ arm-linux-gnueabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/arm-linux-gnueabi/7/lto-wrapper
Target: arm-linux-gnueabi
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 7.3.0-16ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --with-as=/usr/bin/arm-linux-gnueabi-as --with-ld=/usr/bin/arm-linux-gnueabi-ld --program-suffix=-7 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --disable-libquadmath-support --enable-plugin --with-system-zlib --with-target-system-zlib --enable-multiarch --enable-multilib --disable-sjlj-exceptions --with-arch=armv5t --with-float=soft --disable-werror --enable-multilib --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=arm-linux-gnueabi --program-prefix=arm-linux-gnueabi- --includedir=/usr/arm-linux-gnueabi/include
Thread model: posix
gcc version 7.3.0 (Ubuntu/Linaro 7.3.0-16ubuntu3)

而我使用的旧 cross_compiler 是

$ arm-gcc-old -v
Reading specs from /work/gcc-3.4.5-glibc-2.3.6/bin/../lib/gcc/arm-linux/3.4.5/specs
Configured with: /work/tools/create_crosstools/crosstool-0.43/build/arm-linux/gcc-3.4.5-glibc-2.3.6/gcc-3.4.5/configure --target=arm-linux --host=i686-host_pc-linux-gnu --prefix=/work/tools/gcc-3.4.5-glibc-2.3.6 --with-float=soft --with-headers=/work/tools/gcc-3.4.5-glibc-2.3.6/arm-linux/include --with-local-prefix=/work/tools/gcc-3.4.5-glibc-2.3.6/arm-linux --disable-nls --enable-threads=posix --enable-symvers=gnu --enable-__cxa_atexit --enable-languages=c,c++ --enable-shared --enable-c99 --enable-long-long
Thread model: posix
gcc version 3.4.5

我应该怎么做才能使用最新的编译器正确编译代码?s3c2440 上的内核是

# uname -a
Linux (none) 2.6.22.6 #1 Thu May 31 14:02:09 CST 2018 armv4tl unknown

该内核是由最新的 cross_compiler 7.3 编译的

谢谢!

标签: linuxarmcross-compiling

解决方案


推荐阅读