ARM32静态交叉编译并使用pidstat教程
一、前提准备
-
主机环境
- 操作系统:Ubuntu/Debian 系统
- 本教程以
sysstat-12.7.7
为例,可按需替换版本号。
-
交叉编译工具链
sudo apt-get update sudo apt-get install \gcc-arm-linux-gnueabihf \g++-arm-linux-gnueabihf \binutils-arm-linux-gnueabihf \libc6-dev-armhf-cross
-
(可选)目标根文件系统 Sysroot
如果有目标板的根文件系统,解压或挂载到/opt/arm-sysroot
,以便正确找到头文件和库。
二、下载源码
- 克隆官方仓库并切换到指定版本:
git clone https://github.com/sysstat/sysstat.git cd sysstat git checkout 12.7.7
- 若仓库中没有
configure
脚本,可先运行:./autogen.sh
三、配置交叉编译环境
在源码根目录下,导出交叉链工具和静态链接标志:
# 1. 指定交叉编译工具
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++
export AR=arm-linux-gnueabihf-ar
export AS=arm-linux-gnueabihf-as
export LD=arm-linux-gnueabihf-ld
export RANLIB=arm-linux-gnueabihf-ranlib
export STRIP=arm-linux-gnueabihf-strip# 2. 指定 sysroot、优化与静态链接
export SYSROOT=/opt/arm-sysroot
export CFLAGS="--sysroot=$SYSROOT -O2 -static"
export LDFLAGS="--sysroot=$SYSROOT -static"# 3. 运行 configure(prefix 可自定义)
./configure \--host=arm-linux-gnueabihf \--prefix=/opt/sysstat-arm32
说明
--host
指定目标架构,不能用--enable-static
,静态链接由-static
控制。- 若无 sysroot,可去掉相关
--sysroot
参数,但需保证交叉链能找到库和头文件。
四、编译并打包
- 编译
make -j$(nproc)
- 安装到本地临时根目录
make DESTDIR=$HOME/sysstat-arm32-root install
- 打包
cd $HOME/sysstat-arm32-root/opt/sysstat-arm32 tar czvf ~/pidstat-arm32-static.tar.gz bin/pidstat lib/ share/
五、部署到 ARM32 设备
- 拷贝文件
scp ~/pidstat-arm32-static.tar.gz user@arm-device:/tmp ssh user@arm-device cd /usr/local sudo tar xzvf /tmp/pidstat-arm32-static.tar.gz sudo ln -s /usr/local/bin/pidstat /usr/bin/pidstat
- 检查依赖
file /usr/local/bin/pidstat # 应显示 “statically linked”
六、使用示例
登录到 ARM32 设备后,运行:
# 每秒报告一次进程级 I/O 使用,连续 5 次
pidstat -d 1 5# 查看内存与 CPU 使用
pidstat -r -u 2 3
若一切正常,即说明你已成功编译、部署并运行静态链接的 ARM32 版 pidstat
。
如有其它 sysstat
工具需求(如 iostat
、mpstat
),同样放在 bin/
目录下,可一并使用。祝编译顺利!