编译环境
一、下载 openssl、paho.mqtt.c、paho.mqtt.cpp
这里使用的版本分别是
openssl-OpenSSL_1_1_1g.zip paho.mqtt.c-1.3.0.zip paho.mqtt.cpp-1.0.1.zip
二、编译脚本
复制下面脚本保存为 mqtt_install.sh
#! /bin/sh # mqtt_install.sh # 指定编译结果的路径 RESULT_DIR=$(pwd)/result_dir RESULT_SSL=${RESULT_DIR}/ssl_result RESULT_MQTT_C=${RESULT_DIR}/mqtt.c RESULT_MQTT_CPP=${RESULT_DIR}/mqtt.cpp # 指定预案代码目录 SRC_SSL_DIR=openssl-OpenSSL_1_1_1g SRC_MQTT_C_DIR=paho.mqtt.c-1.3.0 SRC_MQTT_CPP_DIR=paho.mqtt.cpp-1.0.1 # 指定交叉编译工具 CROSSS_COMPILE_TOOL=arm-hisiv300-linux- # 解压 unzip openssl-OpenSSL_1_1_1g.zip unzip paho.mqtt.c-1.3.0.zip unzip paho.mqtt.cpp-1.0.1.zip # 1. 编译ssl cd ${SRC_SSL_DIR} # 1.1 配置输出目录和交叉编译器, (linux-generic32表示是32位操作系统,个别文章加了这个选项就不用去掉 -m64,我这里行不通) ./config no-asm shared no-async --prefix=${RESULT_SSL} --cross-compile-prefix=${CROSSS_COMPILE_TOOL} # 1.2 删除 -m64 sed -i 's/-m64//' Makefile # 1.3 添加 CFLAGS+=-mcpu=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4 -mno-unaligned-access -fno-aggressive-loop-optimizations sed -i '/CFLAGS=-DDSO_DLFCN/a\CFLAGS+=-mcpu=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4 -mno-unaligned-access -fno-aggressive-loop-optimizations' Makefile # 1.4 make && make install make && make install cd .. # 2. 编译 paho.mqtt.c-1.2.1 cd ${SRC_MQTT_C_DIR} # 2.1 设置 prefix 目录 sed -i '/ifndef prefix/i\prefix='${RESULT_MQTT_C}'\' Makefile sed -i '/CC ?= gcc/a\CC='${CROSSS_COMPILE_TOOL}'gcc\' Makefile # 2.2 置顶 ssl 库路径 sed -i '/$(CFLAGS)/i\ CFLAGS := -mcpu=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4 -mno-unaligned-access -fno-aggressive-loop-optimizations -I '${RESULT_SSL}'/include \ LDFLAGS := -L '${RESULT_SSL}'/lib\ ' Makefile # 2.3 make # 2.4 拷贝结果 rm ${RESULT_MQTT_C} -rf mkdir -p ${RESULT_MQTT_C}/include mkdir -p ${RESULT_MQTT_C}/lib cp build/output/libpaho-mqtt3a.* ${RESULT_MQTT_C}/lib -far cp src/MQTT*.h ${RESULT_MQTT_C}/include -far cd .. # 3.编译 paho.mqtt.cpp-1.0.1 cd ${SRC_MQTT_CPP_DIR} # 3.1 修改 CMakeList.txt ,使支持 C++11 sed -i '/C++11 build flags/a\add_definitions(-D_GLIBCXX_USE_C99=1)\' CMakeLists.txt # 3.2 # 这里的 DPAHO_MQTT_C_LIBRARIES 指定要使用哪个库,库的类型在 paho-mqtt.c 的 README 里有说到 mkdir build_arm cd build_arm cmake .. -DCMAKE_CXX_COMPILER=${CROSSS_COMPILE_TOOL}g++ \ -DCMAKE_INSTALL_PREFIX=${RESULT_MQTT_CPP} \ -DPAHO_MQTT_C_LIBRARIES=${RESULT_MQTT_C}/lib/libpaho-mqtt3a.so \ -DPAHO_MQTT_C_INCLUDE_DIRS=${RESULT_MQTT_C}/include \ -DOPENSSL_SSL_LIBRARY=${RESULT_SSL}/lib/libssl.so \ -DOPENSSL_INCLUDE_DIR=${RESULT_SSL}/include \ -DOPENSSL_CRYPTO_LIBRARY=${RESULT_SSL}/lib/libcrypto.so \ -DCMAKE_CXX_FLAGS="-std=gnu++11 -mcpu=cortex-a7 -mfloat-abi=softfp -mfpu=neon-vfpv4 -ffunction-sections -mno-unaligned-access -fno-aggressive-loop-optimizations" # 3.3 编译 make && make install
三、执行编译脚本
ubuntu:/home/samba/MQTT$ ls mqtt_install.sh openssl-OpenSSL_1_1_1g.zip paho.mqtt.c-1.3.0.zip paho.mqtt.cpp-1.0.1.zip ubuntu:/home/samba/MQTT$ ./mqtt_install.sh ... ... ... ubuntu:/home/samba/MQTT$ ls mqtt_install.sh openssl-OpenSSL_1_1_1g.zip paho.mqtt.c-1.3.0.zip paho.mqtt.cpp-1.0.1.zip openssl-OpenSSL_1_1_1g paho.mqtt.c-1.3.0 paho.mqtt.cpp-1.0.1 result_dir ubuntu:/home/samba/MQTT$ ls result_dir/ mqtt.c mqtt.cpp ssl_result
执行脚本后会在当前目录生成一个目录 result_dir,里面有 mqtt.c 和 mqtt.cpp
四、注意
4.1 如果编译mqtt.cpp时报错,且与 cmake 有关的,可以更新一下 cmake
https://cmake.org/files/v3.20/
tar -xzvf cmake-3.13.0-Linux-x86_64.tar.gz # 解压出来的包,将其放在 /opt 目录下,其他目录也可以,主要别以后不小心删了 sudo mv cmake-3.13.0-Linux-x86_64 /opt/cmake-3.13.0 # 建立软链接 sudo ln -sf /opt/cmake-3.13.0/bin/* /usr/bin/ # 查看 cmake 版本 cmake --version
4.2 如果编译mqtt.cpp时报如下错误,则可能是没配置好C++11相关的配置
/home/samba/MQTT/paho.mqtt.cpp-1.0.1/build_arm$ make Consolidate compiler generated dependencies of target paho-cpp-objs [ 7%] Building CXX object src/CMakeFiles/paho-cpp-objs.dir/async_client.cpp.o In file included from /home/samba/MQTT/paho.mqtt.cpp-1.0.1/src/mqtt/token.h:29:0, from /home/samba/MQTT/paho.mqtt.cpp-1.0.1/src/mqtt/async_client.h:29, from /home/samba/MQTT/paho.mqtt.cpp-1.0.1/src/async_client.cpp:19: /home/samba/MQTT/paho.mqtt.cpp-1.0.1/src/mqtt/exception.h: In constructor ‘mqtt::exception::exception(int)’: /home/samba/MQTT/paho.mqtt.cpp-1.0.1/src/mqtt/exception.h:56:39: error: ‘to_string’ is not a member of ‘std’ : std::runtime_error("MQTT error ["+std::to_string(code)+"]"), code_(code) {} ^ /home/samba/MQTT/paho.mqtt.cpp-1.0.1/src/mqtt/exception.h: In constructor ‘mqtt::exception::exception(int, const string&)’: /home/samba/MQTT/paho.mqtt.cpp-1.0.1/src/mqtt/exception.h:63:38: error: ‘to_string’ is not a member of ‘std’ std::runtime_error("MQTT error ["+std::to_string(code)+"]: "+msg), ^ make[2]: *** [src/CMakeFiles/paho-cpp-objs.dir/async_client.cpp.o] Error 1 make[1]: *** [src/CMakeFiles/paho-cpp-objs.dir/all] Error 2 make: *** [all] Error 2
解决办法:
在paho.mqtt.cpp-1.0.1目录下的CMakeLists.txt下一行加上
add_definitions(-D_GLIBCXX_USE_C99=1)
交叉编译链 Gcc 4.8.3 加上-D_GLIBCXX_USE_C99=1
才能使用to_string
,然后再重新执行cmake,make,