一、安装openssl
mosquitto的安装依赖openssl里的库libssl.so.1.1和libcrypto.so.1.1。
1. 下载openssl
官方下载网站:https://www.openssl.org/source/,目前官方测试版本已经到3.0,稳定版本是1.1,这里下载的是1.1版本。
2. 解压
解压下载的安装包,进入解压后的文件夹,创建安装目录__install:
tar -zxvf openssl-1.1.1j.tar.gz
cd openssl-1.1.1j/
mkdir __install
3. 编译配置
./config no-asm shared --prefix=$(pwd)/__install
参数说明:
no-asm:是在交叉编译过程中不使用汇编代码代码加速编译过程,因为它的汇编代码是对arm格式不支持的
shared :生成动态连接库
–prefix :指定make install后生成目录的路径,$(pwd)为当前文件夹
4. 修改Makefile文件
一共两处:第一,修改交叉编译工具
##### User defined commands and flags ################################
# We let the C compiler driver to take care of .s files. This is done in
# order to be excused from maintaining a separate set of architecture
# dependent assembler flags. E.g. if you throw -mcpu=ultrasparc at SPARC
# gcc, then the driver will automatically translate it to -xarch=v8plus
# and pass it down to assembler. In any case, we do not define AS or
# ASFLAGS for this reason.
CROSS_COMPILE=mipsel-openwrt-linux-
CC=$(CROSS_COMPILE)gcc
CXX=$(CROSS_COMPILE)g++
CPPFLAGS=
CFLAGS=-Wall -O3
CXXFLAGS=-Wall -O3
LDFLAGS=
EX_LIBS=
把CROSS_COMPILE=设为自己的编译工具,我这里设置的是mipsel-openwrt-linux-,可能需要考虑是否已经将交叉编译的工具链设置到全局路径中,否则这里是不是要改为绝对路径。
第二,去掉工程选项中-m64标志,-m64是针对64位系统的,我们的板子是32位,所以这里去掉。
##### Project flags ##################################################
# Variables starting with CNF_ are common variables for all product types
CNF_CPPFLAGS=-DNDEBUG
#CNF_CFLAGS=-pthread -m64
CNF_CFLAGS=-pthread
#CNF_CXXFLAGS=-std=c++11 -pthread -m64
CNF_CXXFLAGS=-std=c++11 -pthread
CNF_LDFLAGS=
CNF_EX_LIBS=-ldl -pthread
5. 编译安装
make
make install
成功后,编译出来的文件在_install的文件夹,其中include为头文件,lib为程序运行中用到的库
bin include lib share ssl
到这里,openssl安装完毕,接下来安装mosquitto。
二、安装mosquitto
1. 官网下载源码
https://mosquitto.org/download/
这里直接下载源码包,在linux下解压,进入目录
tar -zxvf mosquitto-2.0.8
cd mosquitto-2.0.8/
2. 修改config.mk文件
# Build with SRV lookup support.
WITH_SRV:=no
# Build with websockets support on the broker.
WITH_WEBSOCKETS:=no
# Build mosquitto_sub with cJSON support
WITH_CJSON:=no
.
.
.
STATIC_LIB_DEPS:=
#add
CFLAGS +=-I/{上文openssl安装的路径}/openssl-1.1.1j/__install/include
LDFLAGS += -L/{上文openssl安装的路径}/openssl-1.1.1j/__install/lib -lssl -lcrypto
APP_CPPFLAGS=$(CPPFLAGS) -I. -I../../ -I../../include -I../../src -I../../lib
APP_CFLAGS=$(CFLAGS) -DVERSION=\""${VERSION}\""
APP_LDFLAGS:=$(LDFLAGS)
.
.
.
INSTALL?=install
#prefix?=/usr/local
prefix?=/home/cross_mqtt/mqtt_install
incdir?=${prefix}/include
libdir?=${prefix}/lib${LIB_SUFFIX}
localedir?=${prefix}/share/locale
mandir?=${prefix}/share/man
#STRIP?=strip
STRIP?=mipsel-openwrt-linux-strip
参考网上的做法,把WITH_SRV和WITH_WEBSOCKETS设为no,编译的时候报缺少cJSON文件,这里我也是直接去掉了。后面添加了之前安装的openssl的安装路径,CFLAGS和LDFLAGS对应头文件和库文件。最后是修改默认安装路径,因为这里是交叉编译的库,最好是重新建立目录保存,还有STRIP也需要修改成交叉编译工具的STRIP,最好使用绝对路径。
3. 编译安装
make CC=mipsel-openwrt-linux-gcc CXX=mipsel-openwrt-linux-g++
make install
CC和CXX换成自己的交叉编译工具,最好使用绝对路径。
安装完成后,在mqtt_install目录下可以看到以下文件
bin include lib sbin share
使用时,把include和lib两个目录放在工程目录下,与源码一起编译,然后把lib下的库文件放到开发板即可运行。下一篇通过测试代码,简单了解mqtt的使用。