需求
我需要在 x86 的架构上使用指定的编译工具编译出来可以运行在 arm 架构上的 iperf3。
准备
首先需要有一台 x86 安装了 Linux 的设备,把指定的编译工具下载到这个设备上。 然后添加环境变量
1
2
3
4
# 把编译工具添加到环境变量中
echo 'export TOOLCHAIN_PATH="{path}/aarch64-ca53-linux-gnueabihf-10.4.0"' >> ~/.bashrc
echo 'export PATH="$TOOLCHAIN_PATH/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
编译
编译最重要的一步是需要先配置,指定使用的编译工具,指定编译产物的位置等等。因为我最终运行 iperf 的设备缺少了很多动态库,例如 libz.so,所以我需要在编译的时候直接静态连接到其中。 下边是我使用的编译配置,不同的需要需要不同的编译配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 下载
git clone https://github.com/esnet/iperf.git
cd iperf
# 更新 config(iperf里边的config文件有可能比较老,需要更新一下)
wget -O config/config.sub 'https://gitweb.git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'
wget -O config/config.guess 'https://gitweb.git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'
# 配置
./configure \
--host=aarch64-linux-gnu \ # 指定目标机器架构
CC=aarch64-ca53-linux-gnu-gcc \ # 指定编译工具
CXX=aarch64-ca53-linux-gnu-g++ \
--prefix=$PWD/iperf3 \ # 指定安装位置
LDFLAGS="-static" \ # 需要静态链接某些文件需要加static,另外需要搜索的lib也可以写在这里
CPPFLAGS="" \ # 需要搜索的头文件
LIBS="-l:libz.a -lpthread -ldl" # 需要链接的文件
make
make install
我本来计划编译时把 openssl 也静态编译其中,但是因为我的编译工具不支持编译 openssl.1.1.1g,所以最终没有编译其中,如果想要编译的话可以先把 openssl 编译完成添加到环境变量中然后使用下边的脚本。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
git clone https://github.com/esnet/iperf.git
cd iperf
wget -O config/config.sub 'https://gitweb.git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'
wget -O config/config.guess 'https://gitweb.git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'
./configure \
--host=aarch64-linux-gnu \
CC=aarch64-ca53-linux-gnu-gcc \
CXX=aarch64-ca53-linux-gnu-g++ \
--prefix=$PWD/iperf3 \
LDFLAGS="-static -L$OPENSSL_PATH/lib" \
CPPFLAGS="-I$OPENSSL_PATH/include" \
LIBS="-l:libz.a -lpthread -ldl"
make
make install