基于mips芯片,openwrt平台如何交叉编译htop

发布时间:2021-12-18 11:43:01 作者:小新
来源:亿速云 阅读:482
# 基于MIPS芯片的OpenWRT平台如何交叉编译htop

## 前言

在嵌入式Linux系统中,资源监控工具对于系统维护和性能调优至关重要。htop作为top的增强版,提供了更直观的进程监控体验。本文将详细介绍如何在OpenWRT平台上为MIPS架构交叉编译htop工具。

---

## 一、环境准备

### 1.1 硬件要求
- 支持MIPS架构的路由器/开发板
- 主机系统:推荐x86_64架构的Linux系统(Ubuntu/Debian等)

### 1.2 软件依赖
```bash
sudo apt update
sudo apt install -y build-essential git subversion libncurses5-dev gawk \
     zlib1g-dev unzip python3 wget cmake

1.3 OpenWRT SDK获取

根据目标设备芯片型号下载对应SDK(以MT7621为例):

wget https://downloads.openwrt.org/releases/21.02.1/targets/ramips/mt7621/openwrt-sdk-21.02.1-ramips-mt7621_gcc-8.4.0_musl.Linux-x86_64.tar.xz
tar -xvf openwrt-sdk-*.tar.xz
cd openwrt-sdk-*

二、交叉编译工具链配置

2.1 环境变量设置

export STAGING_DIR=$(pwd)/staging_dir
export PATH=$PATH:$(pwd)/staging_dir/toolchain-mipsel_24kc_gcc-8.4.0_musl/bin

2.2 验证工具链

mipsel-openwrt-linux-gcc -v

应输出类似信息:

gcc version 8.4.0 (OpenWrt GCC 8.4.0 r16325-88151b8303) 

三、htop源码获取与配置

3.1 获取源码

git clone https://github.com/htop-dev/htop.git
cd htop

3.2 生成交叉编译配置

./autogen.sh

3.3 配置编译参数

./configure \
    --host=mipsel-openwrt-linux \
    --prefix=/usr \
    --disable-unicode \
    --enable-static \
    CFLAGS="-I$STAGING_DIR/target-mipsel_24kc_musl/usr/include" \
    LDFLAGS="-L$STAGING_DIR/target-mipsel_24kc_musl/usr/lib"

关键参数说明: - --host:指定目标平台 - --disable-unicode:禁用Unicode支持(减小体积) - CFLAGS/LDFLAGS:指定头文件和库路径


四、解决依赖问题

4.1 常见错误处理

错误1:缺少ncurses库

cp -r $STAGING_DIR/target-mipsel_24kc_musl/usr/include/ncurses* ./ncurses/

错误2:缺少libnl-genl

# 在OpenWRT SDK中编译libnl
make package/libnl/compile V=s

4.2 手动指定库路径

export PKG_CONFIG_PATH=$STAGING_DIR/target-mipsel_24kc_musl/usr/lib/pkgconfig

五、编译与安装

5.1 执行编译

make -j$(nproc)

5.2 检查生成文件

file htop

应显示:

htop: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1...

5.3 精简二进制(可选)

mipsel-openwrt-linux-strip htop

六、部署到目标设备

6.1 传输文件

scp htop root@192.168.1.1:/usr/bin/

6.2 验证运行

在设备终端执行:

htop -v

正常应输出版本信息:

htop 3.2.1 - (C) 2004-2022 Hisham Muhammad

七、常见问题排查

7.1 运行时报错

错误:缺少动态库

解决方案:

# 在设备上安装依赖
opkg update
opkg install libncurses

7.2 界面显示异常

修改TERM环境变量:

export TERM=linux

八、进阶优化

8.1 静态编译

修改configure参数:

LDFLAGS="-static -L$STAGING_DIR/..."

8.2 集成到OpenWRT固件

  1. 在SDK的package目录创建htop目录
  2. 编写Makefile:
include $(TOPDIR)/rules.mk

PKG_NAME:=htop
PKG_VERSION:=3.2.1
PKG_RELEASE:=1

include $(INCLUDE_DIR)/package.mk

define Package/htop
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=Interactive process viewer
  DEPENDS:=+libncurses
endef

define Package/htop/install
    $(INSTALL_DIR) $(1)/usr/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/htop $(1)/usr/bin/
endef

$(eval $(call BuildPackage,htop))

结语

通过本文的步骤,我们成功在OpenWRT平台上为MIPS架构交叉编译了htop工具。该过程涉及工具链配置、依赖管理和交叉编译技巧,这些方法同样适用于其他软件的交叉编译场景。建议在实际操作时根据具体芯片型号调整环境参数。

注意:不同OpenWRT版本可能需要调整SDK路径和库版本,建议参考官方文档进行适配。 “`

(全文约1450字,实际字数可能因代码块格式略有差异)

推荐阅读:
  1. Arm平台交叉编译工具链下载地址
  2. htop的安装

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

htop mips openwrt

上一篇:OpenWrt中服务配置选项的示例代码

下一篇:如何进行springboot配置templates直接访问的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》