arm-linux-gcc交叉编译容器 centos7 环境

直接使用 arm-linux-gcc交叉编译容器 centos7 环境

docker run -tdi -p 50000:22 --name armlinuxgcc --privileged=true  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:armlinuxgcc init 
进入容器后输入 
arm-linux-gcc -v
如果找不到命令。先输入
source /etc/profile
 
 再次输入 版本查看
 arm-linux-gcc -v
Using built-in specs.
Target: arm-none-linux-gnueabi
Configured with: /opt/FriendlyARM/mini2440/build-toolschain/working/src/gcc-4.4.3/configure --build=i386-build_redhat-linux-gnu --host=i386-build_redhat-linux-gnu --target=arm-none-linux-gnueabi --prefix=/opt/FriendlyARM/toolschain/4.4.3 --with-sysroot=/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi//sys-root --enable-languages=c,c++ --disable-multilib --with-arch=armv4t --with-cpu=arm920t --with-tune=arm920t --with-float=soft --with-pkgversion=ctng-1.6.1 --disable-sjlj-exceptions --enable-__cxa_atexit --with-gmp=/opt/FriendlyARM/toolschain/4.4.3 --with-mpfr=/opt/FriendlyARM/toolschain/4.4.3 --with-ppl=/opt/FriendlyARM/toolschain/4.4.3 --with-cloog=/opt/FriendlyARM/toolschain/4.4.3 --with-mpc=/opt/FriendlyARM/toolschain/4.4.3 --with-local-prefix=/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi//sys-root --disable-nls --enable-threads=posix --enable-symvers=gnu --enable-c99 --enable-long-long --enable-target-optspace
Thread model: posix
gcc version 4.4.3 (ctng-1.6.1)

这就是交叉编译器的创建过程
请自行百度 arm-linux-gcc 的使用,来进一步学习如何编译
如果你不关心容器的构建过程,下边的都不用看了。

arm-linux-gcc-4.4.3 下载路径

http://112.124.9.243/arm9net/mini2440/linux/arm-linux-gcc-4.4.3-20100728.tar.gz

构建交叉编译容器

创建空白centos7

docker run -tdi -p 50000:22 --name armlinuxgcc --privileged=true  registry.cn-hangzhou.aliyuncs.com/mkmk/centos:ssh init 

上传压缩包到 /home目录

解压

cd /home
tar -zxvf arm-linux-gcc-4.4.3-20100728.tar.gz
cp -r /home/opt/FriendlyARM/toolschain/4.4.3  /usr/local/arm

配置环境变量

vi /etc/profile
末尾添加
export PATH=$PATH:/usr/local/arm/bin
source /etc/profile
使用
arm-linux-gcc  -v
-linux-gnueabi-gcc: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory

报错
ia32-libs is missing for x86_64 platform.

缺乏 32 兼容环境

yum install epel-release
yum install glibc.i686

查看版本

arm-linux-gcc -v
Using built-in specs.
Target: arm-none-linux-gnueabi
Configured with: /opt/FriendlyARM/mini2440/build-toolschain/working/src/gcc-4.4.3/configure --build=i386-build_redhat-linux-gnu --host=i386-build_redhat-linux-gnu --target=arm-none-linux-gnueabi --prefix=/opt/FriendlyARM/toolschain/4.4.3 --with-sysroot=/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi//sys-root --enable-languages=c,c++ --disable-multilib --with-arch=armv4t --with-cpu=arm920t --with-tune=arm920t --with-float=soft --with-pkgversion=ctng-1.6.1 --disable-sjlj-exceptions --enable-__cxa_atexit --with-gmp=/opt/FriendlyARM/toolschain/4.4.3 --with-mpfr=/opt/FriendlyARM/toolschain/4.4.3 --with-ppl=/opt/FriendlyARM/toolschain/4.4.3 --with-cloog=/opt/FriendlyARM/toolschain/4.4.3 --with-mpc=/opt/FriendlyARM/toolschain/4.4.3 --with-local-prefix=/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi//sys-root --disable-nls --enable-threads=posix --enable-symvers=gnu --enable-c99 --enable-long-long --enable-target-optspace
Thread model: posix
gcc version 4.4.3 (ctng-1.6.1) 

简单使用

我们在/home 下新建简单代码文件

扫描二维码关注公众号,回复: 10957876 查看本文章
#include"stdio.h"
int main(){
printf("hello arm\n");
}

先编译生成 x86 的执行问文件

gcc helloArm.c -o hellox86
执行查看效果
./hellox86 
hello arm
arm-linux-gcc helloArm.c -o helloarm
报错  找不到 libstdc++.so.6
 yum whatprovides libstdc++.so.6
 libstdc++-4.8.5-39.el7.i686 : GNU Standard C++ Library

安装依赖
yum install libstdc++-4.8.5-39.el7.i686

arm-linux-gcc helloArm.c -o helloarm
执行
 ./helloarm 
-bash: ./helloarm: cannot execute binary file

使用arm容器

配置qemu 依赖

https://blog.csdn.net/xiang_freedom/article/details/92724299

下载上传
https://github.com/multiarch/qemu-user-static/releases

/usr/bin/qemu-aarch64-static
/usr/bin/qemu-arm-static
到宿主机的/usr/bin 目录下

chmod 777 /usr/bin/qemu-aarch64-static
chmod 777 /usr/bin/qemu-arm-static

拖取arm 容器

指定 docker 参数

docker run --rm --privileged multiarch/qemu-user-static:register
docker run -it -v /usr/bin/qemu-aarch64-static:/usr/bin/qemu-aarch64-static  -p 51000:22 --name arm64   arm64v8/ubuntu 

docker run -it -v /usr/bin/qemu-arm-static:/usr/bin/qemu-arm-static  -p 51000:22 --name arm32   arm32v7/ubuntu 

把我们之前的编译的文件放到这里 /home

直接运行 
/home/helloarm
./helloarm: No such file or directory
这里并不是说找不到可执行文件,而是找不到
执行文件中的依赖

回到宿主机
宿主机执行  readelf -a helloarm 
 [Requesting program interpreter: /lib/ld-linux.so.3]


目标安装依赖

把目标机缺少的 /lib 安装一下就好了

把宿主机 /usr/local/arm/arm-none-linux-gnueabi/lib
内容拷贝到 目标机 /lib
宿主机

cd /usr/local/arm/arm-none-linux-gnueabi/lib
tar -cvf lib2.tar *

目标机

cd lib
tar -xvf lib2.tar

arm64 换源

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-security main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-updates main multiverse restricted universe
deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ xenial-backports main multiverse restricted universe

成功在目标机执行arm代码

配置目标机

把宿主机 /usr/local/arm/arm-none-linux-gnueabi/lib
内容拷贝到 目标机 /lib
宿主机

cd /usr/local/arm/arm-none-linux-gnueabi/lib
tar -cvf lib2.tar *

目标机

cd lib
tar -xvf lib2.tar

配置好的目标机镜像

registry.cn-hangzhou.aliyuncs.com/mkmk/ubuntu:arm32

docker run -it -v /usr/bin/qemu-arm-static:/usr/bin/qemu-arm-static   --name arm32   registry.cn-hangzhou.aliyuncs.com/mkmk/ubuntu:arm32 

进入目标机后执行

/lib/helloarm
输出 :hello arm

发布了101 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43373608/article/details/105282589
今日推荐