准备

  • M1 CPU 的 Mac
  • Sanp7的协议
  • qt 5.12.9源代码

操作步骤

  • 安装 Brew
# 第一个brew 
arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"


#第二个
arch -arm64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 配置代理和环境变量
alias proxy='export all_proxy=socks5://127.0.0.1:1089'
alias unproxy='unset all_proxy'
export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
alias ibrew='arch -x86_64 /usr/local/bin/brew'

ibrew 安装 gettext

ibrew install gettext

解压

  • 配置 arm编译工具
  • 下载 https://github.com/thinkski/osx-arm-linux-toolchains/releases/download/8.3.0/arm-unknown-linux-gnueabihf.tar.xz
  • https://thinkski.github.io/osx-arm-linux-toolchains/
  • 解压到 /Users/用户名/Desktop/home/arm-unknown-linux-gnueabihf/
# 配置环境变量
export PATH=$PATH:/Users/用户名/Desktop/home/arm-unknown-linux-gnueabihf/bin
  • 下载 qt-everywhere-src-5.12.9.tar.xz 安装
#解压到 /Users/用户名/home/qt-everywhere-src-5.12.9

wget https://download.qt.io/archive/qt/5.12/5.12.9/single/qt-everywhere-src-5.12.9.tar.xz

tar -Jxvf qt-everywhere-src-5.12.9.tar.xz
cd qt-everywhere-src-5.12.9
vim qtbase/mkspecs/linux-arm-gnueabi-g++/qmake.conf

#
# qmake configuration for building with arm-linux-gnueabi-g++
#

MAKEFILE_GENERATOR      = UNIX
CONFIG                 += incremental
QMAKE_INCREMENTAL_STYLE = sublib

QT_QPA_DEFAULT_PLATFORM = linuxfb
QMAKE_CFLAGS += -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard
QMAKE_CXXFLAGS += -O2 -march=armv7-a -mtune=cortex-a7 -mfpu=neon -mfloat-abi=hard

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)

# modifications to g++.conf
QMAKE_CC = arm-unknown-linux-gnueabihf-gcc
QMAKE_CXX = arm-unknown-linux-gnueabihf-g++
QMAKE_LINK = arm-unknown-linux-gnueabihf-g++
QMAKE_LINK_SHLIB = arm-unknown-linux-gnueabihf-g++

# modifications to linux.conf
QMAKE_AR = arm-unknown-linux-gnueabihf-ar cqs
QMAKE_OBJCOPY = arm-unknown-linux-gnueabihf-objcopy
QMAKE_NM = arm-unknown-linux-gnueabihf-nm -P
QMAKE_STRIP = arm-unknown-linux-gnueabihf-strip
load(qt_config)

./configure -prefix /Users/firshme/Desktop/home/tools/5.12.9/arm-qt -opensource \
-confirm-license \
-release  \
-strip  \
-shared  \
-xplatform linux-arm-gnueabi-g++  \
-optimized-qmake  \
-c++std c++11  \
--rpath=no  \
 -pch \
 -skip qt3d -skip qtactiveqt -skip qtandroidextras -skip qtcanvas3d -skip qtconnectivity -skip qtdatavis3d -skip qtdoc -skip qtgamepad -skip qtlocation -skip qtmacextras -skip qtnetworkauth -skip qtpurchasing -skip qtremoteobjects -skip qtscript -skip qtscxml -skip qtsensors -skip qtspeech -skip qtsvg -skip qttools -skip qttranslations -skip qtwayland -skip qtwebengine -skip qtwebview -skip qtwinextras -skip qtx11extras -skip qtxmlpatterns -make libs -make examples -nomake tools -nomake tests -gui -widgets -dbus-runtime --glib=no --iconv=no --pcre=qt --zlib=qt  -no-openssl --freetype=qt  \
--harfbuzz=qt  -no-opengl  -linuxfb --xcb=no --libpng=qt --libjpeg=qt --sqlite=qt  -plugin-sql-sqlite -recheck-all


make -j16
make install
  • 下载 snap7-full-1.4.2.7z
#解压到 /Users/用户名/home/snap7-full-1.4.2

配置Qt编译

  • 配置kit
  • 配置Qt-arm
  • 编译器
  • 测试

编译测试结果进行模拟测试


(base) ➜  build-ArmTools-qt5129_arm-Release ls -al
total 3360
drwxr-xr-x  69 firshme  staff    2208  7  6 16:02 .
drwx------+ 10 firshme  staff     320  7  6 03:25 ..
-rw-r--r--   1 firshme  staff    1611  7  6 01:30 .qmake.stash
-rwxr-xr-x   1 firshme  staff  230672  7  6 16:03 ArmTools #可执执行文件 
-rw-r--r--   1 firshme  staff    1572 12  1  2016 CentOS7-Base-163.repo
-rwxrwxrwx   1 firshme  staff      45  6 29 00:11 M1_Modbus_Address #配置文件
-rw-r--r--   1 firshme  staff  271895  7  6 16:02 Makefile
drwxr-xr-x  11 firshme  staff     352  7  6 01:49 arm-qt # qt依赖
-rw-r--r--   1 firshme  staff   40844  7  6 16:02 cmqtt.o
-rwxrwxrwx   1 firshme  staff      32  6 29 00:19 conf
-rwxr-xr-x   1 firshme  staff  600696  7  6 01:58 libsnap7.so # s7依赖
-rw-r--r--   1 firshme  staff      32  7  6 02:03 machine_code # 机器码
-rw-r--r--   1 firshme  staff    9400  7  6 16:02 main.o
-rw-r--r--   1 firshme  staff    3591  7  6 16:02 moc_cmqtt.cpp
-rw-r--r--   1 firshme  staff   13100  7  6 16:02 moc_cmqtt.o
-rw-r--r--   1 firshme  staff    4014  7  6 16:02 moc_modbus.cpp
-rw-r--r--   1 firshme  staff    6744  7  6 16:02 moc_modbus.o
-rw-r--r--   1 firshme  staff   15450  7  6 16:02 moc_predefs.h
-rw-r--r--   1 firshme  staff   29257  7  6 16:02 moc_qmqtt_client.cpp
-rw-r--r--   1 firshme  staff   21068  7  6 16:02 moc_qmqtt_client.o
-rw-r--r--   1 firshme  staff    5633  7  6 16:02 moc_qmqtt_network_p.cpp
-rw-r--r--   1 firshme  staff    8096  7  6 16:02 moc_qmqtt_network_p.o
-rw-r--r--   1 firshme  staff    7763  7  6 16:02 moc_qmqtt_networkinterface.cpp
-rw-r--r--   1 firshme  staff    8244  7  6 16:02 moc_qmqtt_networkinterface.o
-rw-r--r--   1 firshme  staff    2706  7  6 16:02 moc_qmqtt_router.cpp
-rw-r--r--   1 firshme  staff    4504  7  6 16:02 moc_qmqtt_router.o
-rw-r--r--   1 firshme  staff    4763  7  6 16:02 moc_qmqtt_routesubscription.cpp
-rw-r--r--   1 firshme  staff    4316  7  6 16:02 moc_qmqtt_routesubscription.o
-rw-r--r--   1 firshme  staff    2736  7  6 16:02 moc_qmqtt_socket_p.cpp
-rw-r--r--   1 firshme  staff    3700  7  6 16:02 moc_qmqtt_socket_p.o
-rw-r--r--   1 firshme  staff    5925  7  6 16:02 moc_qmqtt_socketinterface.cpp
-rw-r--r--   1 firshme  staff    9356  7  6 16:02 moc_qmqtt_socketinterface.o
-rw-r--r--   1 firshme  staff       0  7  6 16:02 moc_qmqtt_ssl_socket_p.cpp
-rw-r--r--   1 firshme  staff     784  7  6 16:02 moc_qmqtt_ssl_socket_p.o
-rw-r--r--   1 firshme  staff    2715  7  6 16:02 moc_qmqtt_timer_p.cpp
-rw-r--r--   1 firshme  staff    3640  7  6 16:02 moc_qmqtt_timer_p.o
-rw-r--r--   1 firshme  staff    4030  7  6 16:02 moc_qmqtt_timerinterface.cpp
-rw-r--r--   1 firshme  staff    3960  7  6 16:02 moc_qmqtt_timerinterface.o
-rw-r--r--   1 firshme  staff       0  7  6 16:02 moc_qmqtt_websocket_p.cpp
-rw-r--r--   1 firshme  staff     780  7  6 16:02 moc_qmqtt_websocket_p.o
-rw-r--r--   1 firshme  staff       0  7  6 16:02 moc_qmqtt_websocketiodevice_p.cpp
-rw-r--r--   1 firshme  staff     788  7  6 16:02 moc_qmqtt_websocketiodevice_p.o
-rw-r--r--   1 firshme  staff    7327  7  6 16:02 moc_tcpserver.cpp
-rw-r--r--   1 firshme  staff    8144  7  6 16:02 moc_tcpserver.o
-rw-r--r--   1 firshme  staff    6801  7  6 16:02 moc_tcpsocket.cpp
-rw-r--r--   1 firshme  staff   10692  7  6 16:02 moc_tcpsocket.o
-rw-r--r--   1 firshme  staff    2558  7  6 16:02 moc_utils.cpp
-rw-r--r--   1 firshme  staff    4332  7  6 16:02 moc_utils.o
-rw-r--r--   1 firshme  staff   15404  7  6 16:02 modbus.o
-rw-r--r--   1 firshme  staff   10764  7  6 16:02 qmqtt_client.o
-rw-r--r--   1 firshme  staff   39676  7  6 16:02 qmqtt_client_p.o
-rw-r--r--   1 firshme  staff    9540  7  6 16:02 qmqtt_frame.o
-rw-r--r--   1 firshme  staff    6592  7  6 16:02 qmqtt_message.o
-rw-r--r--   1 firshme  staff   14788  7  6 16:02 qmqtt_network.o
-rw-r--r--   1 firshme  staff    5300  7  6 16:02 qmqtt_router.o
-rw-r--r--   1 firshme  staff   16632  7  6 16:02 qmqtt_routesubscription.o
-rw-r--r--   1 firshme  staff    7824  7  6 16:02 qmqtt_socket.o
-rw-r--r--   1 firshme  staff     776  7  6 16:02 qmqtt_ssl_socket.o
-rw-r--r--   1 firshme  staff    4336  7  6 16:02 qmqtt_timer.o
-rw-r--r--   1 firshme  staff     776  7  6 16:02 qmqtt_websocket.o
-rw-r--r--   1 firshme  staff     784  7  6 16:02 qmqtt_websocketiodevice.o
-rw-r--r--   1 firshme  staff   18612  7  6 16:02 snap7.o
-rw-r--r--   1 firshme  staff    4012  7  6 16:02 snap7_test.o
-rwxr-xr-x   1 firshme  staff      80  7  6 02:08 start_contarner.sh # 启动armv7-gcc测试容器将pwd映射到/home
-rw-r--r--   1 firshme  staff   16708  7  6 16:02 tcpserver.o
-rw-r--r--   1 firshme  staff   16368  7  6 16:02 tcpsocket.o
-rwxr-xr-x   1 firshme  staff     328  7  6 01:54 test_arm.sh # 在容器内执行的测试脚本
-rw-r--r--   1 firshme  staff    8928  7  6 16:02 threadhandle.o
-rw-r--r--   1 firshme  staff    8652  7  6 16:02 utils.o
  • 运行过程记录

(base) ➜  build-ArmTools-qt5129_arm-Release ./start_contarner.sh
WARNING: The requested image's platform (linux/arm/v7) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
root@02906db41196:/# cd home/
root@02906db41196:/home# ./test_arm.sh
TcpServer start
listen successfully
"ff4227151a3541f59514bc249d2d84b5"
"1"
"FLOAT"
3
3
"3"
"FLOAT"
3
3
"5"
"FLOAT"
3
3
"7"
"FLOAT"
3
3
"9"
"FLOAT"
3
3
start successfully...
"conn device 192.168.2.237"
Connected!
"BOX use config "
^Croot@02906db41196:/home#

最后一步

  • 将程序放到arm板子内
# 复制arm-qt的整个目录到`目标板`    /usr/lib/
# 复制 编译好的libsnap7.so 到`目标板`  /usr/lib/

export QT_ROOT=/usr/lib/arm-qt
export QT_QPA_GENERIC_PLUGINS=tslib:/dev/input/event2
export QT_QPA_FONTDIR=/usr/share/fonts
export QT_QPA_PLATFORM_PLUGIN_PATH=$QT_ROOT/plugins
export QT_QPA_PLATFORM=linuxfb:tty=/dev/fb0
export QT_PLUGIN_PATH=$QT_ROOT/plugins
export LD_LIBRARY_PATH=$QT_ROOT/lib:$QT_ROOT/plugins/platforms
export QML2_IMPORT_PATH=$QT_ROOT/qml
export QT_QPA_FB_TSLIB=1


./ArmTools 这个就是 arm的二进制文件

good job。