当前位置:   article > 正文

openwrt rust环境安装_rust openwrt

rust openwrt

本机环境: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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

然后执行命令:

sudo apt-get update
sudo apt-get upgrade
  • 1
  • 2
  • 输入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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

Openwrt安装:

  1. 下载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

  2. 解压openwrt包:tar -Jxf openwrt-sdk-22.03.0-ramips-mt7620_gcc-11.2.0_musl.Linux-x86_64.tar.xz

  3. 配置环境变量:

    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

  4. 编译源码:完了后会在staging_dir目录下生成工具链。

    sudo make menuconfig

    sudo make V=s

  5. SDKstaging_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

  6. 修改用户目录下的~/.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

验证工具链:

  1. 设置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

  2. 写一个hello world程序a.c

    #include <stdio.h>
    
    int main() {
        printf("Hello, c!\n");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
  3. 使用工具链编译该文件:$STAGING_DIR/toolchain-mipsel_24kc_gcc-11.2.0_musl/bin/mipsel-openwrt-linux-gcc -o a-c a.c

Rust交叉编译:

  1. 安装rust环境:curl https://sh.rustup.rs -sSf | sh

  2. 设置环境变量:source $HOME/.cargo/env

  3. 查看安装结果:rustc --version

  4. 安装nightly版本的rust环境:并设置到当前目录

    rustup install nightly
    rustup override set nightly
    
    • 1
    • 2
  5. 查看当前支持的target-cpus:rustc --print target-list

  6. 安装目标支持环境:rustup target add mips-unknown-linux-musl

    • 交叉编译后目标平台CPU架构是mips、厂商unknown、操作系统linux、运行时库musl。
  7. 安装rust源码,供交叉编译rust std:rustup component add rust-src

  8. 安装xargo工具,该工具方便rust进行交叉编译:cargo install xargo

  9. 配置交叉编译的文件:

    $ 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 = []
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  10. 配置交叉编译的文件:

    [target.mipsel-unknown-linux-musl]
    linker = "mipsel-openwrt-linux-gcc"
    ar = "mipsel-openwrt-linux-ar"
    
    • 1
    • 2
    • 3
  11. 编译cargo build --target=mipsel-unknown-linux-musl

  12. 在环境下运行:

在这里插入图片描述

以上所述过程简述如下:首先在官网上选择符合你硬件平台的openwrt的固件。在这里我的目标平台的硬件是mt7620a所以选择的包是该平台下的,将固件烧录进硬件中。其次选择下载该平台对应的SDK,编译SDK后可以通过里面的tools目录下的工具来执行我们的代码。

问题1:

目前使用的交叉编译工具链为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
  • 1
  • 2

在这里插入图片描述

大部分的MIPS和网络字节序一样,为大端模式

ubuntu的虚拟机和arm开发板上都是小端模式

想法:我所对应的网关的openwrt是大端模式,但是我现在使用的openwrt是小端模式,我把我的固件重新烧录后岂不是可以将目标文件改为小端模式就好啦

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/533458
推荐阅读
相关标签