DOCKER打包制作ARM64交叉编译环境,安装蓝牙库等
一、docker file制作基础docker基础镜像
FROM docker.1ms.run/library/ubuntu:24.04
ENV DEBIAN_FRONTEND=noninteractive
# 使用清华大学APT镜像源加速下载
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
apt-get update && \
apt-get install -y \
build-essential \
cmake \
git \
pkg-config \
gcc-aarch64-linux-gnu \
g++-aarch64-linux-gnu \
libbluetooth-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /workspace
ENV CC_aarch64-linux-gnu=aarch64-linux-gnu-gcc
ENV CXX_aarch64-linux-gnu=aarch64-linux-gnu-g++
CMD ["/bin/bash"]
执行docker部署
docker build -t test-cross-compiler .
执行并进入docker
root@ubuntu:/home/workSpace/DOCKER# docker run -it --rm test-cross-compiler
root@e95781ce11d8:/workspace#
root@e95781ce11d8:/workspace#
二、尝试编译,查看缺少工具链
make[2]: *** [CMakeFiles/test.dir/build.make:90: CMakeFiles/test.dir/src/broadcast_test.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
/workspace/test/src/network_test.cpp:13:10: fatal error: nlohmann/json.hpp: No such file or directory
13 | #include <nlohmann/json.hpp>
缺少nlohmann库,安装:
apt update
apt install -y nlohmann-json3-dev
此时发现需要额外的 ARM64 libbluetooth-dev库,
/usr/lib/gcc-cross/aarch64-linux-gnu/13/../../../../aarch64-linux-gnu/bin/ld: cannot find -lbluetooth: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:210: bin/test] Error 1
make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/test.dir/all] Error 2
从ubuntu下载:
wget http://ports.ubuntu.com/ubuntu-ports/pool/main/b/bluez/libbluetooth3_5.72-0ubuntu5_arm64.deb
wget http://ports.ubuntu.com/ubuntu-ports/pool/main/b/bluez/libbluetooth-dev_5.72-0ubuntu5_arm64.deb
拷贝到docker环境中:
docker cp libbluetooth3_5.72-0ubuntu5_arm64.deb e95781ce11d8:workspace a286db183044
docker cp libbluetooth-dev_5.72-0ubuntu5_arm64.deb e95781ce11d8:workspace
查看deb包内容:
root@e95781ce11d8:/workspace# dpkg -c libbluetooth3_5.72-0ubuntu5_arm64.deb | head -10
drwxr-xr-x root/root 0 2024-04-07 07:10 ./
drwxr-xr-x root/root 0 2024-04-07 07:10 ./usr/
drwxr-xr-x root/root 0 2024-04-07 07:10 ./usr/lib/
drwxr-xr-x root/root 0 2024-04-07 07:10 ./usr/lib/aarch64-linux-gnu/
drwxr-xr-x root/root 0 2024-04-07 07:10 ./usr/lib/aarch64-linux-gnu/bluetooth/
drwxr-xr-x root/root 0 2024-04-07 07:10 ./usr/lib/aarch64-linux-gnu/bluetooth/plugins/
-rw-r--r-- root/root 67800 2024-04-07 07:10 ./usr/lib/aarch64-linux-gnu/bluetooth/plugins/sixaxis.so
-rw-r--r-- root/root 271544 2024-04-07 07:10 ./usr/lib/aarch64-linux-gnu/libbluetooth.so.3.19.12
drwxr-xr-x root/root 0 2024-04-07 07:10 ./usr/share/
drwxr-xr-x root/root 0 2024-04-07 07:10 ./usr/share/doc/
因此,可以直接按当前文件夹解压,解压后自动到/usr/lib所在文件夹:
dpkg -x libbluetooth-dev_5.72-0ubuntu5_arm64.deb /
dpkg -x libbluetooth3_5.72-0ubuntu5_arm64.deb /
解压后查看是否部署完毕:
root@e95781ce11d8:/workspace# find /usr -name "libbluetooth.so*" 2>/dev/null
/usr/lib/x86_64-linux-gnu/libbluetooth.so.3
/usr/lib/x86_64-linux-gnu/libbluetooth.so
/usr/lib/x86_64-linux-gnu/libbluetooth.so.3.19.12
/usr/lib/aarch64-linux-gnu/libbluetooth.so.3
/usr/lib/aarch64-linux-gnu/libbluetooth.so
/usr/lib/aarch64-linux-gnu/libbluetooth.so.3.19.12
部署完毕,交叉编译ARM64程序,添加如下代码:
# 设置交叉编译工具链路径(根据实际情况修改)
set(TOOLCHAIN_PATH "/usr/bin" CACHE PATH "Cross compilation toolchain path")
set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/aarch64-linux-gnu-g++)
执行编译脚本,编译完毕
[100%] Linking CXX executable bin/test_bin
[100%] Built target test_bin
查看二进制是否为指定架构:
root@e95781ce11d8:/workspace/test/build/bin# readelf -h lnc_rid
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Position-Independent Executable file)
Machine: AArch64
Version: 0x1
Entry point address: 0x6e80
Start of program headers: 64 (bytes into file)
Start of section headers: 395800 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 30
Section header string table index: 29
Machine字段显示AArch64,为目标ARM64架构,docker编译环境制作没问题
三、保存为tar.gz并打包,验证打包文件
删除原有容器
# 1. 先提交当前容器状态
docker commit a286db183044 test-complete:latest
# 2. 测试镜像内容
docker run -it --rm test-complete:latest /bin/bash -c "
echo '=== 验证当前内容 ==='
echo '工作目录:'; ls -la /workspace/
echo '编译器:'; aarch64-linux-gnu-gcc --version | head -1
echo 'json库:'; find /usr -name 'json.hpp' | head -1
echo '蓝牙库:'; find /usr -name 'libbluetooth.so' | grep aarch64 | head -1
"
# 3. 保存镜像
docker save test-complete:latest | gzip > backup.tar.gz
# 4. 删除原镜像(模拟丢失)
docker rmi test-complete:latest
# 5. 重新加载验证
docker load -i test-complete.tar.gz
# 6. 再次验证内容是否完整
docker run -it --rm test-complete:latest /bin/bash -c "
echo '=== 验证恢复的内容 ==='
echo '工作目录:'; ls -la /workspace/
echo '编译器:'; aarch64-linux-gnu-gcc --version | head -1
echo 'json库:'; find /usr -name 'json.hpp' | head -1
echo '✅ 所有内容都在!'
"

浙公网安备 33010602011771号