1,源码下载:

​https://github.com/eclipse/paho.mqtt.c​

解压:

guoyanzhang@debian:~/test$ unzip paho.mqtt.c-master.zip

2,安装编译用的库

guoyanzhang@debian:~/test/paho.mqtt.c-master$ sudo apt-get install build-essential gcc make cmake cmake-gui cmake-curses-gui
guoyanzhang@debian:~/test/paho.mqtt.c-master$ sudo apt-get install fakeroot fakeroot devscripts dh-make lsb-release
guoyanzhang@debian:~/test/paho.mqtt.c-master$ sudo apt-get install libssl-dev
guoyanzhang@debian:~/test/paho.mqtt.c-master$ sudo apt-get install doxygen graphviz

3,编译

guoyanzhang@debian:~/test/paho.mqtt.c-master$ make
guoyanzhang@debian:~/test/paho.mqtt.c-master$ sudo make install
guoyanzhang@debian:~/test/paho.mqtt.c-master$ make html

4,把头文件拷贝到自己要用的地方(这步根据自己的需要选择做)

guoyanzhang@debian:~/test$ cp paho.mqtt.c-master/src/MQTTAsync.h mqtt_h/
guoyanzhang@debian:~/test$ cp paho.mqtt.c-master/src/MQTTClientPersistence.h mqtt_h/
guoyanzhang@debian:~/test$ cp paho.mqtt.c-master/src/MQTTProperties.h mqtt_h/
guoyanzhang@debian:~/test$ cp paho.mqtt.c-master/src/MQTTReasonCodes.h mqtt_h/
guoyanzhang@debian:~/test$ cp paho.mqtt.c-master/src/MQTTSubscribeOpts.h mqtt_h/

5,编译的时候要用-lpaho-mqtt3a

The Paho C client comprises four shared libraries:

libmqttv3a.so - asynchronous (MQTTAsync)
libmqttv3as.so - asynchronous with SSL (MQTTAsync)
libmqttv3c.so - "classic" / synchronous (MQTTClient)
libmqttv3cs.so - "classic" / synchronous with SSL (MQTTClient)

3a是异步,3as是带ssl的异步,3c是同步,3cs是带ssl的同步。

paho是分异步函数和同步函数的,异步函数是非阻塞的,就是把消息发到broker,函数会立即返回,但是消息并未被执行,而是加入一个队列,等待执行,同步函数是阻塞的,等到消息被执行才返回。异步函数也可以加一些其它的参数变成同步阻塞,总体来说,异步函数包含同步函数。