Installing MQTT for C++ on Raspberry Pi

Easier method:

sudo apt-get install libmosquitto-dev libmosquittopp-dev libssl-dev

You will now be able to include the following in your c++ code.

#include "mosquittopp.h"

As long as you make your code with the following flags.

-l mosquittopp

Setting up websockets protocol for MQTT: (Without this you will not be able to send messages between the webserver and the nodes)

sudo vim /etc/mosquitto/conf.d/websocket.conf

Add the following here:

listener 1883
listener 1884
protocol websockets

Then restart mosquitto

sudo service mosquitto restart

On the web/client side, you can use javascript MQTT to handle the messages.

Follow the tutorial in the js mqtt git but replace the Location.port with 1884 (same as the one you set in the websocket.conf)

More complicated method:

Note that this was done for Raspberry Pi 3 Model B with Jessie Lite, but i’m sure it will work for other Pi models and Debian OS versions too.

Follow the previous post’s setup for: Installing mosquitto MQTT protocol. We just need to install the mosquitto protocol.

Here we try to build the paho.mqtt.c and paho.mqtt.cpp.

First we need to build and install paho.mqtt.c since this will be used by the cpp build. Follow the folder structure since this is the standard setup and this will be used for the cpp setup as well. If you do place it in a non-standard folder, then later on there will be a ./configure where you can set the location.

Install the needed packages:

sudo apt-get install cmake make gcc libssl-dev
mkdir ~/home/pi/git && cd ~/git
git clone
cd paho.mqtt.c
  • *if you want to build static libraries and smaples :

sudo make install

You can try the sample codes to see if they work

Installing paho.mqtt.cpp

cd ~/git
git clone
cd paho.mqtt.cpp

sudo apt-get install autoconf automake libtool make gcc -y
export PAHO_DIR=$PWD
mkdir mybuild && cd mybuild
$PAHO_DIR/configure --enable-samples (see page for more options)
make #this may take a few minutes
sudo make install

Note: The samples will be built inside the mybuild directory you can try running it now.


For more tests go to src/samples and try to make or build the samples:

sudo ldconfig 
g++ -DOPENSSL -I.. -I/usr/local/include -D_NDEBUG -Wall -std=c++11 -O2 -o async_publish async_publish.cpp -L../../lib -L/usr/local/lib -lpaho-mqttpp3 -lpaho-mqtt3a

Running built in samples:

Open new window for same shell, running:


Open another one and run:


You should see the messages being sent by the publisher on the subscriber page.

Possible Errors:

error cannot find -lpaho-mqttpp3: 

Means you forgot to sudo make install

./async_publish: error while loading shared libraries: cannot open shared object file: No such file or directory: 

Run sudo ldconfig to update the libraries

5 thoughts on “Installing MQTT for C++ on Raspberry Pi

  1. I got below error:
    checking for CRYPTO_num_locks in -lcrypto… no
    configure: error: missing crypto library

    Please help to resolve this error


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.