准备一个 Ubuntu LTS 虚拟机 (wsl 可) 作为操作环境。根据说明安装一些包
apt update
apt install build-essential ccache ecj fastjar file g++ gawk \
gettext git java-propose-classpath libelf-dev libncurses5-dev \
libncursesw5-dev libssl-dev python python2.7-dev python3 unzip wget \
python3-distutils python3-setuptools python3-dev rsync subversion \
swig time xsltproc zlib1g-dev
首先下载设备对应 target (platform) 的 OpenWrt 的 SDK(里面包含了 toolchain),以斐讯 K2为例
下载 openwrt-sdk-21.02.0-ramips-mt7620_gcc-8.4.0_musl.Linux-x86_64.tar.xz
解压 SDK 文件夹放到某个目录,例如 /root/openwrt-sdk-21.02.0-ramips-mt7620_gcc-8.4.0_musl.Linux-x86_64
配置环境变量:
export STAGING_DIR=/root/openwrt-sdk-21.02.0-ramips-mt7620_gcc-8.4.0_musl.Linux-x86_64/staging_dir
export PATH=$STAGING_DIR/toolchain-mipsel_24kc_gcc-8.4.0_musl/bin:$PATH
然后可以用 mipsel-openwrt-linux-gcc 和 mipsel-openwrt-linux-g++ 编译程序。
如果是 Makefile :
GCC = mipsel-openwrt-linux-gcc
GPP = mipsel-openwrt-linux-g++
# 将 stdc++ 库也静态编译
#CPPFLAGS = -static-libstdc++
对于构建更复杂的(依赖关系)程序,参考以下资料: