赞
踩
本机环境:Ubuntu 22.04.1 LTS
目标环境:Openwrt
mips 是32位大端字节序
mipsel 是32位小端字节序
mips64el 是64位小端字节序
有些包在新源里头
编辑/etc/apt/sources.list
文件, 在文件最前面添加以下条目(操作前请做好相应备份):
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
然后执行命令:
sudo apt-get update
sudo apt-get upgrade
输入lsb_release -a查看Ubuntu代号名称:
修改sources文件:sudo vim /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
下载openwrt包:wget https://downloads.openwrt.org/releases/22.03.0/targets/ramips/mt7620/openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64.tar.xz
解压openwrt包:tar -Jxf openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64.tar.xz
配置环境变量:
PATH=$PATH:~/Openwrt/openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-11.2.0_musl/bin
export PATH
export LC_ALL=C.UTF-8
编译源码:完了后会在staging_dir
目录下生成工具链。
sudo make menuconfig
sudo make V=s
将SDK
的staging_dir
存到PATH
中使用并查看:
PATH=$PATH:/home/kondl/Openwrt/openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-11.2.0_musl/bin
export PATH
echo $PATH
修改用户目录下的~/.bashrc
进行配置:
export PATH=$PATH:/home/kondl/Openwrt/openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-11.2.0_musl/bin
export STAGING_DIR=$STAGING_DIR:/home/kondl/Openwrt/openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-11.2.0_musl
source ~/.bashrc
设置STAGING_DIR
环境变量为上面工具链目录:export STAGING_DIR=/home/kondl/Openwrt/openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64/staging_dir
。
写一个hello world
程序a.c
:
#include <stdio.h>
int main() {
printf("Hello, c!\n");
}
使用工具链编译该文件:$STAGING_DIR/toolchain-mipsel_24kc_gcc-11.2.0_musl/bin/mipsel-openwrt-linux-gcc -o a-c a.c
安装rust环境:curl https://sh.rustup.rs -sSf | sh
设置环境变量:source $HOME/.cargo/env
查看安装结果:rustc --version
安装nightly版本的rust环境:并设置到当前目录
rustup install nightly
rustup override set nightly
查看当前支持的target-cpus:rustc --print target-list
安装目标支持环境:rustup target add mips-unknown-linux-musl
安装rust源码,供交叉编译rust std:rustup component add rust-src
安装xargo工具,该工具方便rust进行交叉编译:cargo install xargo
配置交叉编译的文件:
$ cat .cargo/config
[build]
target = "mipsel-unknown-linux-uclibc"
[target.mipsel-unknown-linux-uclibc]
linker = "$STAGING_DIR/bin/mipsel-openwrt-linux-gcc"
$ cat Xargo.toml
[target.mips-unknown-linux-uclibc.dependencies.std]
features = []
配置交叉编译的文件:
[target.mipsel-unknown-linux-musl]
linker = "mipsel-openwrt-linux-gcc"
ar = "mipsel-openwrt-linux-ar"
编译cargo build --target=mipsel-unknown-linux-musl
在环境下运行:
以上所述过程简述如下:首先在官网上选择符合你硬件平台的openwrt的固件。在这里我的目标平台的硬件是mt7620a所以选择的包是该平台下的,将固件烧录进硬件中。其次选择下载该平台对应的SDK,编译SDK后可以通过里面的tools目录下的工具来执行我们的代码。
目前使用的交叉编译工具链为mipsel,所以可以直接编译对应于硬件为MIPSEL的硬件,但是如果将编译修改成mips,则会报如下所示错误。
/home/kondl/Openwrt/openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-11.2.0_musl/bin/../lib/gcc/mipsel-openwrt-linux-musl/11.2.0/../../../../mipsel-openwrt-linux-musl/bin/ld: /home/kondl/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libcompiler_builtins-40fafdb6a2d34c43.rlib(compiler_builtins-40fafdb6a2d34c43.compiler_builtins.3ff2b44e-cgu.91.rcgu.o): compiled for a big endian system and target is little endian
/home/kondl/Openwrt/openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-11.2.0_musl/bin/../lib/gcc/mipsel-openwrt-linux-musl/11.2.0/../../../../mipsel-openwrt-linux-musl/bin/ld: /home/kondl/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libcompiler_builtins-40fafdb6a2d34c43.rlib(compiler_builtins-40fafdb6a2d34c43.compiler_builtins.3ff2b44e-cgu.92.rcgu.o): endianness incompatible with that of the selected emulation
大部分的MIPS和网络字节序一样,为大端模式
ubuntu的虚拟机和arm开发板上都是小端模式
想法:我所对应的网关的openwrt是大端模式,但是我现在使用的openwrt是小端模式,我把我的固件重新烧录后岂不是可以将目标文件改为小端模式就好啦
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。