克隆mosquitto源代码
首先需要检查是否已经安装了openssl和cJSON,没有安装则先安装
git clone https://github.com/eclipse/mosquitto.git
编译安装
cd mosquitto
mkdir build && cd build/
cmake ..
执行到cmake …时,提示如下
sunrise@ubuntu:~/Repo/mosquitto/build$ cmake ..
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenSSL: /usr/lib/aarch64-linux-gnu/libcrypto.so (found version "1.1.1f")
-- WITH_DLT = OFF
-- Found cJSON: /usr/include
-- TRUE
-- Looking for getaddrinfo_a in anl
-- Looking for getaddrinfo_a in anl - found
CMake Error at man/CMakeLists.txt:46 (message):
xsltproc not found: manpages cannot be built
-- Configuring incomplete, errors occurred!
See also "/home/sunrise/Repo/mosquitto/build/CMakeFiles/CMakeOutput.log".
我们需要安装一下xsltproc和docbook-xsl,执行以下命令即可
sudo apt update
sudo apt install xsltproc
sudo apt install docbook-xsl
再次执行cmake …
sunrise@ubuntu:~/Repo/mosquitto/build$ cmake ..
-- WITH_DLT = OFF
-- TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/sunrise/Repo/mosquitto/build
然后编译安装mosquito
make
sudo make install
运行测试
在终端运行以下指令则可开启MQTT服务
mosquitto
新开一个终端,启动订阅者
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
mosquitto_sub -t test
再新开一个终端,运行发布者
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
mosquitto_pub -h localhost -t test -m "hello world"
每运行一次则在订阅者终端打印一次hello world